当鼠标进入jquery时,如何停止fadein和fadeout动画?

时间:2012-11-23 06:22:57

标签: javascript jquery

当鼠标进入jquery时,如何停止淡入淡出和淡出动画? 我有这样的代码

function fadeItIn() {
    $('.banner_outer1').fadeIn(time, function () {
       $('.banner_outer1').mouseenter(function(){
           //stop animation
           });
       $('.banner_outer1').mouseout(function(){
           //start animation
           });     
        setTimeout(fadeItOut, 1400);
       //fadeItOut();
    });
}

function fadeItOut() {
    $('.banner_outer1').fadeOut(time, function () {
        $('.banner_outer1').html(banner_outer2);
        banner_outer3 = banner_outer2;
        banner_outer2 = banner_outer1;
        banner_outer1 = banner_outer3;
        fadeItIn();
    });
}

我想在鼠标进入div时停止动画,当鼠标离开div时恢复动画。我怎么能在jquery做什么?

3 个答案:

答案 0 :(得分:1)

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
        function bind()
        {
            $('.fader').bind('fade-cycle', function() {
                $(this).fadeOut('fast', function() {
                    $(this).fadeIn('fast', function() {
                        $(this).trigger('fade-cycle');
                    });
                });
            });
        }
        bind(); // binding fade-cycle trigger to .fader
        $('.fader').trigger('fade-cycle'); // starting animation by triggering fade-cycle event

        $('.fader').mouseover(function(){
            $('.fader').unbind('fade-cycle'); // stopping animation by unbinding the fade-cyle
        });
        $('.fader').mouseout(function(){
            // restart animation by rebinding and triggering the fade-cycle event
            bind();
            $(this).trigger('fade-cycle');
        });
    });
</script>

<div class="fader">
    paste your content here that you want to animate (fadein & fadeout continuously)
</div>

答案 1 :(得分:0)

$('.banner_outer1').hover(
  handlerIn,
  handlerOut
);

function handlerIn(){
    // do some stuff here on mouseenter
}

function handlerOut(){
    // do some stuff here on mouseout
}

http://api.jquery.com/hover/

答案 2 :(得分:0)

你可以尝试在mouseover事件中设置一个布尔变量。 并用它来停止动画

var shouldAnimate;

$('.banner_outer1').mouseOver(function(){
  shouldAnimate = false;
});

$('.banner_outer1').mouseLeave(function(){
  shouldAnimate = true;
});

function fadeItIn() {
  if (shouldAnimate){
    $('.banner_outer1').fadeIn(time, function () {
       $('.banner_outer1').mouseenter(function(){
           });
       $('.banner_outer1').mouseout(function(){
           });     
    });
   }
    setTimeout(fadeItOut, 1400);
}

function fadeItOut() {
   if (shouldAnimate){
    $('.banner_outer1').fadeOut(time, function () {
        $('.banner_outer1').html(banner_outer2);
        banner_outer3 = banner_outer2;
        banner_outer2 = banner_outer1;
        banner_outer1 = banner_outer3;
        fadeItIn();
    });
  }
}