悬停时快速浏览图像,动画速度不规则
上下文:我有一块图像,绝对位于彼此之上。鼠标悬停时,通过添加一个 css 类将图像移动到堆的顶部,图像会一个接一个地显示。当到达最后一个图像时,将显示第一个图像,并且这种情况将持续到鼠标移出为止。
参考:这里有一个参考站点,可以看到类似的效果。 (将鼠标悬停在图像块上)。
HTML:
<div id="flicker">
<img src="example1.gif" />
<img src="example2.gif" />
<img src="example3.gif" />
</div>
代码:
var flickerImg = jQuery('#flicker img');
jQuery('#flicker').hover(
function() {
flicker = setInterval(function() {
if (flickerImg.last().hasClass('active')) {
flickerImg.removeClass('active').first().addClass('active');
}
else {
jQuery('#flicker img.active').removeClass('active')
.next().addClass('active');
}
}, 100);
},
function() {
clearInterval(flicker);
});
问题:经过几次迭代后,动画似乎加快了速度。是否有更好的编码方法,或者进行更改以提供更一致的动画?
Context: I've got a block of images, absolutely positioned on top of each other. On mouseover the images are shown, one after the other, by adding a css class which moves the image to the top of the pile. When the last image is reached the first image is shown, and this continues until mouseout.
Reference: Here is a reference site to see a similar effect. (mouseover the image block).
HTML:
<div id="flicker">
<img src="example1.gif" />
<img src="example2.gif" />
<img src="example3.gif" />
</div>
Code:
var flickerImg = jQuery('#flicker img');
jQuery('#flicker').hover(
function() {
flicker = setInterval(function() {
if (flickerImg.last().hasClass('active')) {
flickerImg.removeClass('active').first().addClass('active');
}
else {
jQuery('#flicker img.active').removeClass('active')
.next().addClass('active');
}
}, 100);
},
function() {
clearInterval(flicker);
});
Question: The animation seems to speed up after a few iterations. Is there a better way of coding this, or a change to give a more consistent animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设置了一个示例,说明如何模仿参考站点 http://jsfiddle.net/brianflanagan /kLJd3/。我不确定我是否看到您所经历的速度加快,尽管有时感觉它正在加速。我让它运行了 5 分钟左右,看看效果是否持续,但没有。我怀疑这可能更多的是一种视错觉。我在 Chrome 11 中进行了测试,并尝试了不同的间隔设置,看看它是否有任何区别,但无论间隔如何,它似乎都能一致地执行。
I set up an example of how I might do it to mimic the reference site at http://jsfiddle.net/brianflanagan/kLJd3/. I'm not sure I'm seeing the speed up that you're experiencing, though at times it did feel like it was speeding up. I left it running for 5 minutes or so to see if the effect continued, but it didn't. I suspect that it may be more of an optical illusion than anything else. I tested in Chrome 11 and I tried different interval settings to see if it made any difference, but it seemed to execute consistently for me regardless of the interval.