如何简化我的 jQuery 动画?
如何简化我的 jQuery 动画?最好的方法是什么?
代码:
$("#nav").animate({opacity:0.2},1000);
$("#sub_nav").animate({opacity:0.2},1000);
$("#user_links").animate({opacity:0.2},1000);
$("#logo").animate({opacity:0.2},1000);
$(".top_buttons").animate({opacity:0.2},1000);
$(".pageheaders").animate({opacity:0.2},1000);
$(".heading_sub_text").animate({opacity:0.2},1000);
$("#copyright").animate({opacity:0.2},1000);
$("#footer_links").animate({opacity:0.2},1000);
How can I simplify my jQuery animation? What is the best way?
Code:
$("#nav").animate({opacity:0.2},1000);
$("#sub_nav").animate({opacity:0.2},1000);
$("#user_links").animate({opacity:0.2},1000);
$("#logo").animate({opacity:0.2},1000);
$(".top_buttons").animate({opacity:0.2},1000);
$(".pageheaders").animate({opacity:0.2},1000);
$(".heading_sub_text").animate({opacity:0.2},1000);
$("#copyright").animate({opacity:0.2},1000);
$("#footer_links").animate({opacity:0.2},1000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要么为每个元素指定一个类(或一个附加类):
要么将所有元素放在一个选择器中,并用逗号分隔。
或者两者的组合,仅添加当前没有类的类。
Either give each of the elements a class (or an additional class):
Or place all your elements in one selector, separated by commas.
Or a combination of the two, adding a class just the ones that don't currently have a class.
既然您似乎正在对一组元素进行动画处理,那么您是否可以将所有这些元素包装在一个 div 中,然后只对该 div 进行动画处理?
根据您的需要,您可能还需要查看 jQuery 工具的 Expose 功能。它基本上使背景变暗并突出显示(或暴露)特定对象。如果这更像是您想要完成的任务,那么还有一个覆盖工具。
Since it appears that your are animating a group of elements, could you possibly wrap all of them in a div and then just animate that div?
Depending upon your needs, you may also want to look at jQuery Tools' Expose functionality. It basically dims the background and highlights (or exposes) a particular object. The also have an overlay tool if that's more like what you are attempting to accomplish.
patrick dw 的建议将大大清理你的 jQuery 代码。在他的建议中,我认为第一个是最好的。然而,我要指出的是,强调选择器/效果优化(而不是外观改进)要重要得多。
但是,请考虑以下代码。
这将创建
div.container
并将 1000 个div.anim
副本放入其中。执行此操作,然后比较$('.container').fadeOut()
与$('.anim').fadeOut()
的性能。对包含的 div 进行动画处理比对其许多子项进行动画处理要快几个数量级。如果可能的话,如果您想以相同的方式为包装器的所有子级设置动画,请始终为包装器设置动画。What patrick dw suggests will clean up your jQuery code substantially. Of his suggestions, I consider the first one the best. However, I will make a point that it is far more important to emphasize selector/effect optimization (over cosmetic improvements).
However, consider the following code.
This creates
div.container
and puts 1000 copies ofdiv.anim
inside it. Do this, then compare the performance of$('.container').fadeOut()
versus$('.anim').fadeOut()
. Animating a containing div is orders of magnitude faster than animating its many children. If possible, always animate out a wrapper if you want to animate all of its children in an identical fashion.