2012-02-11 24 views
2

5秒ごとに画像を変更する画像スライダーがありますが、div imgにマウスを合わせるとスライダーを一時停止します。ホバー上の 'theRotator'機能を一時停止して、マウスがdivの外に移動した後に再起動する方法はありますか?皆さんありがとう!要素ホバーの一時停止機能

function theRotator() { 
    //Set the opacity of all images to 0 
    $('div.rotator ul li').css({opacity: 0.0}); 

    //Get the first image and display it (gets set to full opacity) 
    $('div.rotator ul li:first').css({opacity: 1.0}); 

    //Call the rotator function to run the slideshow, 

    setInterval('rotate()',5000); 

} 

$(document).ready(function() {  
    //Load the slideshow 
    theRotator(); 
    $('div.rotator').fadeIn(1000); 
    $('div.rotator ul li').fadeIn(1000); // tweek for IE 
}); 
+0

何らかの理由で私はあなたが2回それを実行すると思います?なぜですか? jquery.comで.delay() - >それを読んでみるといいと思います。 – IamStalker

答えて

0

var interval; 

function theRotator() { 
    $('div.rotator ul li').css({opacity: 0.0}); 
    $('div.rotator ul li:first').css({opacity: 1.0}); 
    interval = setInterval(rotate, 5000); 
} 

$(document).ready(function() { 
    theRotator(); 
    $('div.rotator').fadeIn(1000); 
    $('div.rotator ul li').fadeIn(1000); // tweek for IE 

    $('div.rotator ul li').hover(function() { 
     clearInterval(interval); 
    }, 
    function() { 
     interval = setInterval(rotate, 5000); 
    }); 
}); 
+0

もう一度試してみました。 – ajodom10

+0

これはうまくいきました! – ajodom10

1

あなたはsetInterval関数によって返される間隔ハンドルを取得することをお勧めします。また、私はmouseentermouseleaveを使いました。あなたが望む機能に適しているからです。

function theRotator() { 
    //Set the opacity of all images to 0 
    $('div.rotator ul li').css({opacity: 0.0}); 

    //Get the first image and display it (gets set to full opacity) 
    $('div.rotator ul li:first').css({opacity: 1.0}); 

    //Call the rotator function to run the slideshow, 

    window.rotatorInterval = setInterval(rotate,5000); 
} 

$(document).ready(function() { 

    //Load the slideshow 
    theRotator(); 

    $('div img').mouseenter(function(){ 
     clearInterval(window.rotatorInterval); 
    }).mouseleave(function() { 
     window.rotatorInterval = setInterval(rotate, 5000); 
    }); 

    $('div.rotator').fadeIn(1000); 
    $('div.rotator ul li').fadeIn(1000); // tweek for IE 
}); 
0

あなたは

var rotate = true; 

$('img').on({ 
    mouseenter: function() { 
     rotate = false; 
    }, 
    mouseleave: function() { 
     rotate = true; 
    } 
});​ 

ような何かをし、あなたのrotate()機能でそのフラグをチェックすることができますが、単にこのように、マウスオーバーのタイムアウトをオフにして、マウスアウトでそれを再作成する必要があり

関連する問題