我应该使用/避免jQuery动画吗? [英] Should I use/avoid jQuery animate?

查看:96
本文介绍了我应该使用/避免jQuery动画吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题让人想起来回答这一个,OP希望应用此转换:

A question popped to mind answering to this one, where OP wanted to apply this transition:

test.css({transition: 'height 1s linear', height: '100px'});

然后回到这个:

test.css({transition: 'none', height: '0px'});



他说他需要从js完成,并要求为了更好的主意,我提供了这种方法:


He said he needed it to be done from js, and asked for a better idea, so I offered this approach:

test.animate({height: '100px'}, 1000, 'linear', function(){
    test.css({transition: 'none', height: '0px'});
});

这是我的jsFiddle: https://jsfiddle.net/mqaunyvp/1/

Here is my jsFiddle: https://jsfiddle.net/mqaunyvp/1/

@Matrix评论说JS和jQuery动画少了比原生CSS优化,这是正确的,但我认为不是在该上下文中的主题。

@Matrix commented that JS and jQuery animations are less optimised than native CSS, which is right, but I think not on topic in that context.

所以这就是问题:

以我建议的方式完成这项工作是否正确?或者你会建议反对吗?为什么?

Is it right to accomplish this the way I suggested? Or would you advise against it? Why?

推荐答案

所有评论的结论:

jQuery 不是为动画对象 创建的,所以它可能很慢而且有点滞后(取决于你的内容和数量)重新制作动画)。但是,在大多数情况下,使用 jQuery .animate()函数滚动页面效果非常好。

jQuery was not created for animating objects, so it may be slow and laggy (depending on what and how much you're animating). But scrolling a page with jQuery's .animate() function for example works very good in most cases.

对于更高级的动画 (如移动对象或同时执行复杂或多个动画),请使用库 velocity

如果您只想为文本样式/颜色或下拉菜单设置动画 ,则使用CSS过渡更快(更容易)IMO /动画。

If you just want to animate text style/color or a slide-down menu, it's faster (and easier IMO) to use CSS transitions/animations.

所以这取决于你在做什么。希望这会有所帮助!

So it depends on what you're doing. Hope this helps!

这篇关于我应该使用/避免jQuery动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆