【发布时间】:2011-12-02 20:36:20
【问题描述】:
我有一个幻灯片,它为每个图像制作 10 个切片并为这些切片设置动画。即,opera,chrome 运行动画很好,但在 Firefox 中,动画太慢,我尝试了不同的计算机,但仍然相同。我使用“for”循环创建切片,该循环创建 10 个新 div,每个 div 具有不同的背景位置属性。我怎样才能为 Firefox 解决这个问题?谢谢。
我添加了一些代码
//part of creating slices
for(imageN=0;imageN<imageCount;imageN++)
{
$('#image').append("<div name=slices class=slices></div>");
slicesCreate(imageN);
}
//#image is main div contains slice divs.
//Here is sliceCreate function
/*In this function, it takes how many images added to slideshow, takes all
sources for each image and creates slices*/
function slicesCreate(imageN)
{
var i = 0;
var nleft = 0;
var image= $("#image img:eq("+imageN+")").attr("src");
for(i=0;i<10;i++)
{
$('.slices:eq('+imageN+')').append("<div name=slice"+i+" class=slice></div>");
$(".slices:eq("+imageN+") .slice[name=slice"+i+"]").css({
backgroundImage: "url("+image+")",
backgroundPosition: nleft + "px 0px",
backgroundRepeat: "no-repeat",
backgroundSize: twidth
});
/*sliceCount is a global variable that calculated when page loaded for display
resolution*/
nleft -=sliceCount;
}
}
/* And here is one of the animation code, this code runs for every slice */
function animateSlices(current,next,slice,animid,delay)
{
setTimeout(function() {
$(".slices:eq("+next+") .slice[name=slice"+slice+"]").css({
display: "block",
marginTop: "-700px"
});
$(".slices:eq("+next+") .slice[name=slice"+slice+"]").animate({
marginTop: "0px"
},1000);
$(".slices:eq("+current+") .slice[name=slice"+slice+"]").animate({
marginTop: "700px"
},1000);
}, delay);
}
以及点击动画按钮时调用的函数
function anim()
{
if(!animating)
{
animating = true;
var j = 0;
var delay = 0;
current = $('.slices:visible').index('.slices');
var siz = $('.slices').size();
var cSize = $('.slices:visible').size();
var txen = 2;
var rand = parseInt(Math.random()*3);
var last = $('.slices:last').index('.slices');
if(cSize>1)
return;
//this part is calculating id of next image
if(siz > 1 && current!= last)
{
txen = current + 1;
}
else if(siz == 1)
{
txen = current;
}
else if(siz > 1 && current== last)
{
txen = 0;
}
//this part is makin next image slices visible and changes z-index for animation
$(".slices:eq("+txen+")").css({
display: "block",
zIndex: "92"
});
$(".slices:eq("+current+")").css({
zIndex: "91"
});
//this part calls animateSlices function for every next image slices
for(j=0;j<10;j++)
{
$(".slices:eq("+txen+") .slice[name=slice"+j+"]").css({
marginTop: "0px"
});
animateSlices(current,txen,j,rand,delay);
/*current = this image, txen = next image, j = slice number, rand = animation id,
delay = delay for animation */
if(rand==0)
delay += 150;
else
delay += 50;
}
}
else
return;
}
编辑:我已将动画中的“marginTop”更改为“top”,并将每个切片的位置更改为绝对,现在问题已解决,不再滞后。我可能在使用 jquery 代码定位 div 或同时更改大量边距时遇到了一些错误,这可能是一些滞后的原因,但正如我所说,它只发生在 firefox 中,而不是 ie、opera 或 chrome。不知道是什么导致了这个滞后问题,但是当我解决这个问题时,我会在这里写下我的解决方案。现在我将更改marginTop 的最高值。谢谢大家的解答。
【问题讨论】:
-
你为什么不用api.jquery.com/animate?
-
您的动画代码看起来如何?一个例子真的有助于理解这个问题。
-
您将在每个浏览器中体验不同的动画速度。看看你在 IE 7 中的动画,然后看看你认为 FF 有多慢。无论如何,你需要粘贴你的动画代码,因为你的描述你的方法对我来说听起来很奇怪,我做了很多 jquery 动画。
-
我已经添加了部分代码,这可能有助于理解。它为每个图像创建 1 个切片 div 和 10 个切片 div,然后为每个切片设置动画。
-
我想看看处理动画的代码。这看起来就像创建元素的代码。根据您的处理方式,速度可能会有很大差异。
标签: jquery performance firefox animation