如何简化我的 jQuery 动画?

发布于 2024-09-14 09:26:31 字数 478 浏览 1 评论 0原文

如何简化我的 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 技术交流群。

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

发布评论

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

评论(3

薔薇婲 2024-09-21 09:26:31

要么为每个元素指定一个类(或一个附加类):

$('.someNewClass').animate({opacity:0.2},1000);

要么将所有元素放在一个选择器中,并用逗号分隔。

$("#nav,#sub_nav,#user_links,#logo,.top_buttons,.pageheaders,.heading_sub_text,#copyright,#footer_links").animate({opacity:0.2},1000);

或者两者的组合,仅添加当前没有类的类。

// These get .someNewClass: #nav,#sub_nav,#user_links,#logo,#copyright,#footer_links
$(".someNewClass,.top_buttons,.pageheaders,.heading_sub_text").animate({opacity:0.2},1000);

Either give each of the elements a class (or an additional class):

$('.someNewClass').animate({opacity:0.2},1000);

Or place all your elements in one selector, separated by commas.

$("#nav,#sub_nav,#user_links,#logo,.top_buttons,.pageheaders,.heading_sub_text,#copyright,#footer_links").animate({opacity:0.2},1000);

Or a combination of the two, adding a class just the ones that don't currently have a class.

// These get .someNewClass: #nav,#sub_nav,#user_links,#logo,#copyright,#footer_links
$(".someNewClass,.top_buttons,.pageheaders,.heading_sub_text").animate({opacity:0.2},1000);
叹梦 2024-09-21 09:26:31

既然您似乎正在对一组元素进行动画处理,那么您是否可以将所有这些元素包装在一个 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.

江挽川 2024-09-21 09:26:31

patrick dw 的建议将大大清理你的 jQuery 代码。在他的建议中,我认为第一个是最好的。然而,我要指出的是,强调选择器/效果优化(而不是外观改进)要重要得多。

但是,请考虑以下代码。

var c = $('<div class="container" />').appendTo($('body'))
for(var i = 0; i < 1000; i++) {
  c.append($('<div />')
   .css('height', 30)
   .css('width', 30)
   .css('borderWidth', 2)
   .css('borderStyle', 'solid')
   .addClass('anim'))
}

这将创建 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.

var c = $('<div class="container" />').appendTo($('body'))
for(var i = 0; i < 1000; i++) {
  c.append($('<div />')
   .css('height', 30)
   .css('width', 30)
   .css('borderWidth', 2)
   .css('borderStyle', 'solid')
   .addClass('anim'))
}

This creates div.container and puts 1000 copies of div.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.

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