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()は、 clearInterval()を使用して現在の間隔/タイムアウトをクリアするために使用できるintervalIDを返します。

あなたはこのようなことをすることができます:

$(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 に答える