jQuery:Stop()事件处理有关Animate()的问题已完成 假设我有两个功能:

jQuery:Stop()事件处理有关Animate()的问题已完成 假设我有两个功能:,jquery,event-handling,jquery-animate,Jquery,Event Handling,Jquery Animate,根据这些规则,.a将在.b停止时停止 mouseover-ed和animate({“top”:“100px”},{duration:5000}) 被触发 但我希望它在.b鼠标悬停时发出警报(“a”)一次 然后触发动画({“top”:“100px”},{duration:5000}) 但是,我不能这样设置第二个函数: 如果.b在.a完成后将鼠标置于上方,则会发出警报(“a”)两次 制作动画 我尝试了stop()的jumpToEnd参数,但效果并不理想 每当.b鼠标悬停时,.a的动画将立即完成

根据这些规则,
.a
将在
.b
停止时停止
mouseover
-ed和
animate({“top”:“100px”},{duration:5000})
被触发

  • 但我希望它在
    .b
    鼠标悬停时发出
    警报(“a”)
    一次 然后触发
    动画({“top”:“100px”},{duration:5000})

  • 但是,我不能这样设置第二个函数:
    • 如果
      .b
      .a
      完成后将
      鼠标置于
      上方,则会发出
      警报(“a”)
      两次
      制作动画
    我尝试了
    stop()
    jumpToEnd参数
    ,但效果并不理想 每当
    .b
    鼠标悬停时,
    .a
    动画将立即完成,并且
    .a
    将移至
    左侧:100%
    。我想让它在触发第二个
    动画之前
    停止
    ,并发出
    警报(“a”)



    如果有人能提供一个快速简便的解决方案,我将不胜感激

    您可以根据
    :animated
    对元素进行过滤,以检查元素当前是否正在设置动画

    $('.b').mouseover(function () {
    
        // if NOT animating, then terminate
        if (!$('.a').is(':animated')) { return; }
    
        // carry on
    
    });
    
    $(".b").mouseover(function(){
            //something happens! e.g
            alert("a");
            $(".a").stop().animate({"top":"100px"},{duration:5000});
        });
    
    $('.b').mouseover(function () {
    
        // if NOT animating, then terminate
        if (!$('.a').is(':animated')) { return; }
    
        // carry on
    
    });
    
    $(".a").animate(
            {"left":"100%"},
            {duration:10000, complete:myAfterWork} // Use the function name only
    );
    
    $(".b").mouseover(function(){
        myAfterWork();
        $(".a").stop().animate({"top":"100px"},{duration:5000});
    });
    
    function myAfterWork()
    {
      if($(".a").is(":animated"))
      {
        //Access $(".a") here and do your job
    
        //Note if you use $(this) in place of $(".a") it will be different 
        //when called from .a's animation than from .b's animation
      }
    
    }