简体   繁体   English

带有淡入和淡出的幻灯片显示(jQuery)

[英]slideshow with fade-in and fade-out (jquery)

please help me and tell me what's wrong here.... also i dont want to use jq plugin when i setInterval the code the pictures are coming with a different way sorry for my bad english 请帮助我,告诉我这里出了什么问题...。我也不想在我设置间隔时使用jq插件,图片中的代码以另一种方式出现,对不起我的英语不好

<code>
    $(function(){
        function slideshow(){
        $('.img4').fadeOut(2800,null,function(){
                $('.img3').fadeOut(2800,null,function(){
                    $('.img2').fadeOut(2800,null,function(){
                        $('.img1').fadeIn(2800,null,function(){
                            $('.img2').fadeIn(2800,null,function(){
                                $('.img3').fadeIn(2800,null,function(){
                                    $('.img4').fadeIn(2800,null,function(){
                                        $('.img4').fadeOut(2800)
                                    })
                                })
                            })
                        })
                    })  
                })
            })
        }
        //slideshow()
        setInterval(slideshow,1000);

</code>

There are quite a few things wrong here... 这里有很多错误...

  1. Dont use individual selectors to get the image. 不要使用单独的选择器来获取图像。 Figure them out programatically - otherwise you have to change your code each time you add/remove an image. 以编程方式找出它们-否则,每次添加/删除图像时都必须更改代码。
  2. Dont repeat the callback code for the your callbacks, make that a function that takes parameters. 不要为您的回调重复该回调代码,而是使该函数具有参数。
  3. Dont use setInterval instead use setTimeout and have your callbacks clear and initialize new timeouts when necessary. 不要使用setInterval而是使用setTimeout ,并在需要时清除回调并初始化新的超时。

That said these are more best issues dealing with coding style and best practices, not necessarily responsible for your actual problem. 也就是说,这些是涉及编码样式和最佳实践的最佳问题,不一定会对您的实际问题负责。 But you never state exactly what the problem is. 但是您永远不会确切说明问题所在。 If you could edit your question and elaborate that would be useful. 如果您可以编辑问题并加以阐述,那将很有用。

I'm sure you have a very good reason for building a custom slide show but if I can suggest using a plugin to avoid unmaintainable code? 我确定您有很好的理由来构建自定义幻灯片,但是是否可以建议使用插件来避免代码无法维护?

http://jquery.malsup.com/cycle/ http://jquery.malsup.com/cycle/

You are starting a new slideshow every second, so after a while you will have hundreds of slideshows trying to animate the same objects. 您每秒都在开始一个新的幻灯片放映,因此一段时间后,您将有数百个试图为相同对象设置动画的幻灯片放映。

Instead of having an interval that starts new slideshows, start the next when the first finishes: 不用让间隔开始新的幻灯片放映,而是在第一个结束时开始下一个幻灯片放映:

$(function(){
  function slideshow(){
    $('.img4').fadeOut(2800,null,function(){
      $('.img3').fadeOut(2800,null,function(){
        $('.img2').fadeOut(2800,null,function(){
          $('.img1').fadeIn(2800,null,function(){
            $('.img2').fadeIn(2800,null,function(){
              $('.img3').fadeIn(2800,null,function(){
                $('.img4').fadeIn(2800,null,function(){
                  slideshow();
                })
              })
            })
          })
        })  
      })
    })
  }
  slideshow();
});

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

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