开发者

jquery stop image rotation on mouseover, start on mouseout / hover

I have built a jQuery rotator to rotate through 3 divs and loop them. I would like to add the functionality on mouse over to "freeze" the current div and then start again on mouse out.

I've thought about setting a variable to false at the start of the function and setting it true when it's on it's current frame but I've got my self a bit confused.

I've also tried to use the hover function but when using the in and out handlers, I'm confused as to how to stop, restart the animation.

  function ImageRotate() {
 var CurrentFeature = "#container" + featureNumber;

 $(CurrentFeature).stop(false, true).delay(4500).animate({'top' : '330px'}, 3000);


 var featureNumber2 = featureNumber+1;
 if ( featureNumber == numberOfFeatures) {featureNumber2 = 1}
 var NewFeature = "#container" + featureNumber2;
 $(NewFeature).stop(false, true).delay(4500).animate({'top' : '0px'}, 3000); 

 var featureNumber3 = featureNumber-1;
 if ( featureNumber == 1) {featureNumber3 = numberOfFeatures};
 var OldFeature = "#container" + featureNumber3;
 $(OldFeature).stop(false, true).delay(4500).css('top' , '-330px'); 

 set开发者_JAVA技巧Timeout('if (featureNumber == numberOfFeatures){featureNumber = 1} else {featureNumber++}; ImageRotate2()', 7500)};

Any help would be greatly appreciated!! Thanks, Matt


If you were to add this code:

var timerId = null;
function startRotation() {
    if (timerId) {
        return;
    }
    timerId = setInterval('if (featureNumber == numberOfFeatures){featureNumber = 1} else {featureNumber++}; ImageRotate2()', 7500);
}
function stopRotation() {
    if (!timerId) {
        return;
    }
    clearInterval(timerId);
    timerId = null;
}

and replace the last line of your code block with a simple call to startRotation();, then you could call stopRotation and startRotation when the mouse hovers over/leaves your element:

$('your-element-selector').hover(stopRotation, startRotation);


It's not clear what you are trying to do with the three divs without seeing the HTML and more code, so I think a basic example might help you better (demo).

HTML

<div class="test">image: <span></span></div>

Script

$(document).ready(function(){

 var indx = 0, loop, numberOfFeatures = 5;

 function imageRotate(){
  indx++;
  if (indx > numberOfFeatures) { indx = 1; }
  $('.test span').text(indx);
  loop = setTimeout( imageRotate , 1000 );
 }
 imageRotate();

 $('.test').hover(function(){
  clearTimeout(loop);
 }, function(){
  imageRotate();
 });

})


changed things up a little bit, here is how I ended up doing it. `

 var animRun = false;
var rotateHover = false;

function startRotation() {

            rotateHover = false;
            ImageRotate();
        }

    function stopRotation() {
        rotateHover = true;
        clearTimeout();
    }

    function ImageRotate() {

    if (rotateHover == false){
    animRun = true;

    var CurrentFeature = "#container" + featureNumber;

    $(CurrentFeature).stop(false, true).animate({'top' : '330px'}, featureDuration, function(){animRun = false;});

    var featureNumber2 = featureNumber+1;
    if ( featureNumber == numberOfFeatures) {featureNumber2 = 1}
    var NewFeature = "#container" + featureNumber2;
    $(NewFeature).stop(false, true).animate({'top' : '0px'}, featureDuration); /* rotate slide 2 into main frame */

    var featureNumber3 = featureNumber-1;
    if ( featureNumber == 1) {featureNumber3 = numberOfFeatures};
    var OldFeature = "#container" + featureNumber3;
    $(OldFeature).stop(false, true).css('top' , '-330px'); /*bring slide 3 to the top*/

    //startRotation();

    setTimeout('if (featureNumber == numberOfFeatures){featureNumber = 1} else {featureNumber++}; if (rotateHover == false){ImageRotate2()};', featureDelay);
    };
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜