1

所以我有这个代码:

$(document).ready(function(){
  $("#slideshow > div:gt(0)").hide();
     setInterval(function() {
     $("#slideshow > div:first")
          .fadeOut(1000)
          .next()
          .fadeIn(1000)
          .end()
          .appendTo("#slideshow");
},  3000);
});

可以 100% 正常工作.. 我在哪里添加.hover()幻灯片的功能以在悬停时暂停?

4

1 回答 1

5

方法setInterval()返回一个 intervalID,您可以使用clearInterval()清除当前间隔/超时。

你可以这样做:

$(document).ready(function() {

    var timer;

    $("#slideshow > div:gt(0)").hide();

    $("#slideshow")
        // when mouse enters, clear the timer if it has been set
        .mouseenter(function() {
            if (timer) { clearInterval(timer) }
        })
        // when mouse goes out, start the slideshow
        .mouseleave(function() {
            timer = setInterval(function() {
                $("#slideshow > div:first")
                    .fadeOut(1000)
                    .next()
                    .fadeIn(1000)
                    .end()
                    .appendTo("#slideshow");
            }, 3000);
        })
        // trigger mouseleave for initial slideshow starting
        .mouseleave();

});​

演示

于 2012-06-06T11:58:03.883 回答