简化多余的 jQuery 代码

发布于 2024-10-16 20:14:07 字数 2325 浏览 3 评论 0原文

我想简化页脚弹出/工具提示动画的 jQuery 代码,因为它是多余的并且不是很可扩展。我找到了这篇文章: 用于简化的 jQuery 代码 但无法弄清楚如何将其应用到我的情况。谢谢!

<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);
});
    });

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 © 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);
});
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2024-10-23 20:14:07

如果这些将是悬停元素上的唯一类,您可以这样做:

$('.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);

或者甚至像这样短一点:

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

请注意 .bind('click',false); 需要 jQuery 1.4.3或稍后。

...或者更好的是,使用 delegate()( docs) 方法。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文