Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify redundant jQuery code

I would like to simplify my jQuery code for a footer popup/tooltip animation because it is redundant and not very extensible. I found this post: jQuery code to simplify but could not figure out how to apply it to my situation. Thanks!

<div id="footer-wrap">
    <div id="footer">
        <ul>
            <li class="copyright"><a href="#" >copyright</a></li>
            <li class="facebook"><a href="#" target="_blank">facebook</a></li>
            <li class="twitter"><a href="#" target="_blank">twitter</a></li>
            <li class="wordpress"><a href="#" target="_blank">blog</a></li>
        </ul>       
    </div>
    <div class="popup">
        <p class="popup-wordpress"><span class="popup-icon"></span><span class="popup-text">Check Out Our Blog</span></p>
        <p class="popup-twitter"><span class="popup-icon"></span><span class="popup-text">Follow us on Twitter</span></p>
        <p class="popup-facebook"><span class="popup-icon"></span><span class="popup-text">Become a fan on Facebook</span></p>
        <p class="popup-copyright"><span class="popup-text">Copyright &copy; 2011<br />All Rights Reserved</span></p>
    </div>
</div>


$(function() {
   $('.copyright').hover(
    function() {
        $('.popup-copyright').stop().animate({ marginTop: -52 }, 100);
    },
    function() {
        $('.popup-copyright').stop().animate({ marginTop: 0 }, 100);
});
$('.copyright').click(function() {
        return false
    });
$('.facebook').hover(
    function() {
        $('.popup-facebook').stop().animate({ marginTop: -52 }, 100);
    },
    function() {
        $('.popup-facebook').stop().animate({ marginTop: 0 }, 100);
});
$('.twitter').hover(
    function() {
        $('.popup-twitter').stop().animate({ marginTop: -52 }, 100);
    },
    function() {
        $('.popup-twitter').stop().animate({ marginTop: 0 }, 100);
});
$('.wordpress').hover(
    function() {
        $('.popup-wordpress').stop().animate({ marginTop: -52 }, 100);
    },
    function() {
        $('.popup-wordpress').stop().animate({ marginTop: 0 }, 100);
});
    });
like image 988
Robert Pessagno Avatar asked Jan 20 '23 13:01

Robert Pessagno


1 Answers

If those will be the only class on the elements being hovered, you could do this:

$('.copyright,.facebook,.twitter,.wordpress').hover(
    function() {
        $('.popup-' + this.className).stop().animate({ marginTop: -52 }, 100);
    },
    function() {
        $('.popup-' + this.className).stop().animate({ marginTop: 0 }, 100);
});
$('.copyright').bind('click',false);

or even a little shorter like this:

$('.copyright,.facebook,.twitter,.wordpress').hover( function(e) {
    $('.popup-' + this.className).stop().animate({ marginTop: e.type === 'mouseenter' ? -52 : 0 }, 100);
});
$('.copyright').bind('click',false);

Note that .bind('click',false); requires jQuery 1.4.3 or later.

...or better yet, use the delegate()(docs) method.

$('#footer').delegate('li','hover' function(e) {
    $('.popup-' + this.className).stop().animate({ marginTop: e.type === 'mouseenter' ? -52 : 0 }, 100);
});
$('.copyright').bind('click',false);
like image 61
user113716 Avatar answered Jan 28 '23 20:01

user113716