优化 jQuery 悬停代码以实现更好的性能(更流畅的动画)
最近,我花了一些时间阅读 jQuery 优化技巧,这无疑有助于提高我的脚本的性能。
表现不佳(可能还有 Chrome。)
然而,我的网站上有这个特色新闻部分,鼠标悬停时会将更多信息滑入到位,并且该部分在除 Safari 之外的任何浏览器中都 我相信,这是因为它在动画之前对每个鼠标悬停/鼠标悬停事件进行了相当多的 DOM 遍历和计算。
我的问题很简单:有没有办法优化下面的代码,让动画运行得更流畅?
$('#featuredSide .featuredBox,#featuredBottom .featuredBox,#archiveVideos .featuredBox').hover(function(){
var boxHeight = parseInt($(this).css('height'))-8;
var bottomOrSide = $(this).parents("div[id^='featured']").attr("id")
var captionPos = 76;
var captionHeight = parseInt($(this).find(".videoCaption").css('height'));
var animateTo = boxHeight-captionHeight;
$(".videoCaption", this).stop().animate({top:animateTo + 'px'},{queue:false,duration:160});
}, function() {
$(".videoCaption", this).stop().animate({top:captionPos},{queue:false,duration:100});
});
由于我正在开发的网站尚未发布,所以我已 上传新闻部分的屏幕截图,让您了解它的样子。
谢谢!
Lately I've been spending some time reading about jQuery optimization tips which has certainly helped increase the performance of my scripts.
However, I've got this featured news section on my site, which on mouse hover slides more information into place, and this section doesn't perform very well in any browser except Safari (and probably Chrome too, then.)
The reason for this, I believe, is that it's doing quite some DOM traversing and calculations on every mouseover/mouseout event before animating.
My question is simple: Is there any way to optimize the code below so that the animation runs more smoothly?
$('#featuredSide .featuredBox,#featuredBottom .featuredBox,#archiveVideos .featuredBox').hover(function(){
var boxHeight = parseInt($(this).css('height'))-8;
var bottomOrSide = $(this).parents("div[id^='featured']").attr("id")
var captionPos = 76;
var captionHeight = parseInt($(this).find(".videoCaption").css('height'));
var animateTo = boxHeight-captionHeight;
$(".videoCaption", this).stop().animate({top:animateTo + 'px'},{queue:false,duration:160});
}, function() {
$(".videoCaption", this).stop().animate({top:captionPos},{queue:false,duration:100});
});
Since the site I'm working on hasn't been published yet I've uploaded a screenshot of the news section to give you an idea of what it looks like.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种解决方案是记忆所有计算。
不要直接调用hover,使用“each”,计算,然后应用“hover”。
因此(我尝试尽可能少地更改代码):
这个解决方案将使我之前的答案有些无意义(它们并不重要,尽管仍然适用)。不过,请记住进行配置文件。
Another solution would be to memoize all calculation.
Don't call hover directly, use "each", calculate, and then apply the "hover".
Thus (I tried to change the code as little as possible):
This solution would make my previous answer somewhat moot (they won't matter much, although still applicable). Remember to profile, though.
对于初学者来说,可以执行公共子表达式消除,例如,而不是
多次 调用,将该对象存储在变量中并使用该变量。
另一方面,可以内联一次性使用的变量。
有些人可能称其为过早优化,但由于已经指出代码很慢,所以我认为这并不为时过早。
那里似乎没有使用 BottomOrSide 变量。
至于选择器,可以用这个替换整个长的东西吗?
For starter, it is possible to do Common subexpression elimination, so for example instead of calling
multiple times, store that object in a variable and use that variable instead.
On the other hand, single use variables can be inlined.
Some may call them premature optimization, but since it is already stated that the code is slow, I don't think it's premature.
The bottomOrSide variable doesn't seem to be used there.
As for the selector, it is possible to replace the whole long thing with this?
有些工作你要重复做几次,这会对你造成伤害。很难说多少,但是试试这个...
看起来你的 dom 结构在使用这个面板的所有不同位置都必须不同,因为你的代码似乎必须做相当多的工作才能找到使用适当的 dom 位。如果可能的话,我建议在所有不同位置使 DOM 结构相同,然后在代码中使用该结构。
如果这是不可能的,请尝试为每个位置编写此函数的唯一版本 - 并不理想,但如果它解决了您的性能问题,则可能是值得的。
You do some of the work several times over, which will be hurting you. How much is hard to say, but try this...
It also looks like your dom structure must be different in all the different locations that this panel is used, as your code seems to have to do a fair bit of work to find the appropriate bits of dom to use. If possible, I would recommend making the DOM structure the same in all the different locations, and then making use of that structure in the code.
If that isn't possible, try coding a unique version of this function for each location - not ideal, but if it solves your performance problem it might be worth it.