简体   繁体   English

jquery骑自行车穿过图像

[英]jquery cycling through images

I have a page where I cycle through the images on the page using this code: 我有一个页面,我使用此代码循环浏览页面上的图像:

$('#summaryContainer img:gt(0)').hide();
    setInterval(function(){
        $('#summaryContainer :first-child').fadeOut(summary.speed)
            .next('img').fadeIn(summary.speed)
            .end().appendTo('#summaryContainer');}, 
      summary.pause);

Works fine...until, you leave the page and come back. 工作得很好......直到你离开页面然后回来。 Then before the cycle begins it flashes the last image, then goes to the first. 然后在循环开始之前它会闪烁最后一张图像,然后转到第一张图像。 Any idea what is causing that? 知道是什么造成的吗?

Try setting the css display property: 尝试设置css显示属性:

#summaryContainer img {
    display: none;
}

Then, instead of your first line (which we take care of in css), fadeIn your first image: 然后,而不是你的第一行(我们在css中处理),淡入你的第一个图像:

$('#summaryContainer img:gt(0)').fadeIn();

This should make all images hidden and then fadeIn that first image smoothly. 这应该使所有图像隐藏,然后平滑地淡化第一个图像。

So, I guess the whole thing would look like this: 所以,我猜整个事情看起来像这样:

 $('#summaryContainer img:gt(0)').fadeIn();
 setInterval(function(){
    $('#summaryContainer :first-child')
         .fadeOut(summary.speed)
         .next('img').fadeIn(summary.speed)
         .end().appendTo('#summaryContainer');}, 
 summary.pause);

Plus that css rule. 加上那个css规则。 Something like that. 这样的事情。

jsBin demo jsBin演示

Using .stop() and .fadeTo() and img:first is the key 使用.stop().fadeTo()img:first是关键

$('#summaryContainer img:gt(0)').hide();
setInterval(function(){
    $('#summaryContainer img:first').stop().fadeTo(summary.speed, 0)
        .next('img').fadeTo(summary.speed, 1)
        .end().appendTo('#summaryContainer');
},summary.pause);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM