最近我花了一些时间阅读关于jQuery优化技巧,这无疑帮助提高了我的脚本的性能。但是,我在我的网站上有这个特色新闻部分,它在鼠标悬停的位置上放置了更多信息,而且这部分在除Safari之外的任何浏览器中表现都不是很好(当然也可能是Chrome)。 )优化jQuery悬停代码以更好地执行(更平滑的动画)
我相信这样做的原因是它在动画之前对每个mouseover/mouseout事件进行了相当多的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});
});
由于我工作的网站尚未公布我已经uploaded a screenshot of the news section给你的是什么样子的想法。
谢谢!
这是一个相当优雅的解决方案。我一定会试着看看它在性能方面是否会产生更好的结果。 再次感谢! – heintore 2009-10-07 09:56:10