简化多余的 jQuery 代码
我想简化页脚弹出/工具提示动画的 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 © 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这些将是悬停元素上的唯一类,您可以这样做:
或者甚至像这样短一点:
请注意
.bind('click',false);
需要 jQuery 1.4.3或稍后。...或者更好的是,使用
delegate()
( docs) 方法。If those will be the only class on the elements being hovered, you could do this:
or even a little shorter like this:
Note that
.bind('click',false);
requires jQuery 1.4.3 or later....or better yet, use the
delegate()
(docs) method.