【问题标题】:Is there a way to make this slideshow move automatically?有没有办法让这个幻灯片自动移动?
【发布时间】:2011-05-15 11:44:15
【问题描述】:

这是我们使用的幻灯片:

http://www.littlewebthings.com/projects/blinds/

这是 JS 文件:

http://www.littlewebthings.com/projects/blinds/js/jquery.blinds-0.9.js

但是,此幻灯片没有自动浏览每张图片的设置。您仍然必须单击其下方的数字才能查看图像。有人知道要在 javascript 代码中添加什么以使其自动通过每个图像吗?

谢谢!

【问题讨论】:

    标签: javascript recursion lambda functional-programming closures


    【解决方案1】:

    此解决方案使用闭包和递归。

    var SlideChanger = function(seconds_each) {
      var index = -1; 
      // on the first cycle, index will be set to zero below
      var maxindex = ($(".change_link").length) - 1; 
      // how many total slides are there (count the slide buttons)
      var timer = function() { 
      // this is the function returned by SlideChanger
        var logic = function() { 
        // this is an inner function which uses the 
        // enclosed values (index and maxindex) to cycle through the slides
          if (index == maxindex) 
            index = 0; // reset to first slide
          else
            index++; // goto next slide, set index to zero on first cycle
          $('.slideshow').blinds_change(index); // this is what changes the slide
          setTimeout(logic, 1000 * seconds_each); 
          // schedule ourself to run in the future
        }
        logic(); // get the ball rolling
      }
      return timer; // give caller the function
    }
    
    SlideChanger(5)(); // get the function at five seconds per slide and run it
    

    我们在这里所做的是将内部函数timer 暴露在定义它的函数之外(SlideChanger)。这个函数 (timer) 可以访问在 SlideChanger (indexmaxindex) 中定义的变量。

    现在我们已经设置了封闭环境中的变量和返回调用者的函数,我们可以在logic 中设置逻辑引擎。当logic 运行时,它使用indexmaxindex 来确定接下来应该显示哪张幻灯片,显示幻灯片,并安排自己在将来再次运行。

    当被调用时,返回函数调用logic 让球滚动。然后logic 无限期地重复,每次运行时都会安排自己在未来运行。

    因此,总而言之,我们使用数字参数 x 调用 SlideChanger。它返回一个函数,在被调用后,将每x 秒更改一次幻灯片。

    写相同概念的另一种方式。

    (function(seconds_each) {
      var index = -1; 
      // on the first cycle, index will be set to zero below
      var maxindex = ($(".change_link").length) - 1; 
      // how many total slides are there (count the slide buttons)
      return function() { 
      // this is the function returned by SlideChanger
        var logic = function() { 
        // this is an inner function which uses the 
        // enclosed values (index and maxindex) to cycle through the slides
          if (index == maxindex) 
            index = 0; // reset to first slide
          else
            index++; // goto next slide, set index to zero on first cycle
          $('.slideshow').blinds_change(index); // this is what changes the slide
          setTimeout(logic, 1000 * seconds_each); 
          // schedule ourself to run in the future
        }
        logic(); // get the ball rolling
      }
    })(5)(); // get the function at five seconds per slide and run it
    

    JavaScript 是一种很好的语言,具有许多函数式编程结构,例如高阶函数(创建函数或接受函数作为参数的函数)和匿名函数。欲了解更多信息,请参阅http://www.ibm.com/developerworks/web/library/wa-javascript.html

    【讨论】:

    • 感谢发帖。但是你能指导我在 JS 文件的哪个位置插入这个吗?
    • 在jquery.blinds-0.9.js底部试试
    • 不起作用。我在“})(jQuery);”之前插入了代码块在末尾。 :?我做对了吗?
    • 在那之后尝试一下 - 在文件的非常底部。
    • 还是不行。将它放在“})(jQuery);”之后的最底部。 :|
    【解决方案2】:

    ecounysis 的解决方案可行,但它过于复杂。这是一种使用 setInterval 取模的更简单方法,并且不将其包装在额外的函数中:

    function startSlideshow(ms) {
        var index = -1;
        var count = $(".change_link").length - 1;
        return setInterval(function() {
            index = (index + 1) % count;
            $('.slideshow').blinds_change(index);
        }, ms);
    }
    

    【讨论】:

    【解决方案3】:

    在快速浏览源代码后,我没有看到用于自动推进幻灯片的内置 API。但是,您可以使用其他幻灯片,例如:

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

    【讨论】:

      【解决方案4】:

      您只需要在其上包含一个计时器并调用blinds_change 事件。这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-21
        • 1970-01-01
        • 2020-06-28
        • 2015-07-17
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多