最近我一直在阅读有关 jQuery 优化技巧的文章,这些技巧无疑有助于提高我的脚本的性能。
但是,我在我的网站上有这个精选新闻部分,鼠标悬停时可以将更多信息滑入适当的位置,并且该部分在除 Safari 之外的任何浏览器中都表现不佳(那么可能还有 Chrome。)
我相信这样做的原因是它在动画之前对每个 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});
});
由于我正在开发的网站尚未发布,因此我上传了新闻部分的屏幕截图,让您了解它的外观。
谢谢!