I'm currently working on my portfolio site and it's all great, but I'm unsure how to DRY my jQuery. I'm repeating myself a lot in the jQuery and it's just messy.
HTML:
<div id="footer-start" class="row">
<div class="col-sm-12 col-md-12">
<ul class="nav nav-pills footer-nav">
<li><a href="#" class="loadHide" id="clickPortfolio">Portfolio</a></li>
<li><a href="#" class="loadHide" id="clickAbout">About</a></li>
<li><a href="#" class="loadHide" id="clickContact">Contact</a></li>
</ul>
<span>Click to expand</span>
<img src="img/arrow.png" alt="" class="loadHide">
</div>
</div>
<div id="footer-portfolio" class="row">
<div class="col-sm-6 col-sm-offset-4 col-md-6 col-md-offset-4">
<a class="lightbox" href="img/" title=""><img src="img" width="150px" height="150px" /></a>
<a class="lightbox" href="img/" title="(WIP)"><img src="img/" width="80px" height="150px" /></a>
</div>
</div>
<div id="footer-about" class="row">
<div class="col-sm-8 col-sm-offset-2 col-md-8 col-md-offset-2">
<h3>About me<small></small></h3>
<p>Paragraph</p>
</div>
</div>
<div id="footer-contact" class="row">
<h3>Questions<small></small></h3>
<div class="col-sm-8 col-sm-offset-2 contact-form">
<form id="contact" method="post" action="send.php" class="form" role="form">
<div class="row">
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required autofocus />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required />
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Message" rows="7"></textarea>
<br />
<div class="row">
<div class="col-xs-12 col-md-12 form-group">
<button class="btn btn-primary pull-right" type="submit">Submit</button>
</div>
</div>
</form>
</div>
</div>
jQuery:
$(document).ready(function() {
var $footerPortfolio = $('#footer-portfolio'),
$footerAbout = $('#footer-about'),
$footerContact = $('#footer-contact'),
$footerSent = $('#footer-contact-sent'),
$clickPortfolio = $('#clickPortfolio'),
$clickAbout = $('#clickAbout'),
$clickContact = $('#clickContact'),
$arrowImg = $('#footer-start img'),
$arrowTxt = $('#footer-start span'),
$loadHide = $('.loadHide');
sentUrl = window.location.href;
$loadHide.hide();
$arrowImg.hover(function() {
$arrowTxt.show('fade', 800);
},function() {
$arrowTxt.hide('fade');
});
$clickPortfolio.click(function() {
event.preventDefault();
$loadHide.slideUp(500);
$footerPortfolio.slideToggle(500);
});
$clickAbout.click(function() {
event.preventDefault();
$loadHide.slideUp(500);
$footerAbout.slideToggle(500);
});
$clickContact.click(function() {
event.preventDefault();
$loadHide.slideUp(500);
$footerContact.slideToggle(500);
});
if (sentUrl.search("#sent") >= 0) {
$footerContact.slideUp(500);
$footerSent.slideDown(800).delay(1000).slideUp(800);
}
});
I got a bit cleaner, and would love some feedback on this solution.
$(document).ready(function() {
var $footerPortfolio = $('#footer-portfolio'),
$footerAbout = $('#footer-about'),
$footerContact = $('#footer-contact'),
$footerSent = $('#footer-contact-sent');
var $clickPortfolio = $('#clickPortfolio'),
$clickAbout = $('#clickAbout'),
$clickContact = $('#clickContact');
var $arrowImg = $('#footer-start img'),
$arrowTxt = $('#footer-start span');
var $loadHide = $('.loadHide');
var sentUrl = window.location.href;
$loadHide.hide();
$arrowImg.hover(function() {
$arrowTxt.show('fade', 800);
},function() {
$arrowTxt.hide('fade');
});
function myClick(obj) {
event.preventDefault();
if (obj == $footerPortfolio) {
$footerAbout.slideUp(500);
$footerContact.slideUp(500);
obj.slideToggle(500);
} else if (obj == $footerAbout) {
$footerPortfolio.slideUp(500);
$footerContact.slideUp(500);
obj.slideToggle(500);
} else if (obj == $footerContact) {
$footerPortfolio.slideUp(500);
$footerAbout.slideUp(500);
obj.slideToggle(500);
}
}
$clickPortfolio.click(function() {
myClick($footerPortfolio); }
);
$clickAbout.click(function() {
myClick($footerAbout); }
);
$clickContact.click(function() {
myClick($footerContact); }
);
if (sentUrl.search("#sent") >= 0) {
$footerContact.slideUp(500);
$footerSent.slideDown(800).delay(1000).slideUp(800);
}
});
Even better:
function myClick(obj) {
event.preventDefault();
if (obj.css('display') == 'none'){
$loadHide.slideUp(500);
obj.slideToggle(500);
} else if (obj.css('display') != 'none') {
obj.slideUp(500);
}
}