【问题标题】:jQuery animation sticking on quick hoverjQuery动画坚持快速悬停
【发布时间】:2017-10-16 23:55:13
【问题描述】:

如果您将鼠标缓慢悬停在元素上,动画将正常工作。绿色层从左侧重叠,然后从顶部开始,黄色层与绿色层重叠。当鼠标离开元素时,这种重叠应该会自行撤消,首先撤消黄色重叠,然后撤消绿色重叠。

但是如果光标悬停在它上面的速度太快,动画就会卡在黄色重叠处,直到你重新鼠标悬停然后鼠标移出。我尝试在每个.animate 方法之前添加.stop(false, true) jQuery 方法,这是我读到的解决了类似问题的方法,但这没有用。我通过在 .animate 函数之前链接它来尝试它,我尝试了它的几乎所有变体,在所有函数上,以及 .stop(true,true);

如果鼠标悬停部分在光标离开元素之前没有完成,有没有办法阻止鼠标悬停部分触发?

$(document).ready(function() {
  $('#con').hover(
    function() { // handlerIn
      $('#crossX').animate({'width': '115px'}, function() {
        $('#crossY').animate({'height': '115px'})
      })
    },
    function() { // handlerOut
      $('#crossY').animate({'height': '15px'}, function() {
        $('#crossX').animate({'width': '15px'})
      })
    }
  )
});
#con {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 130px;
  height: 130px;
  overflow: hidden;
  //background-color: black;
}
#one {
  width: 100px;
  height: 100px;
  background-color: lightgrey;
  color:black
}
#crossX {
  position: absolute;
  top: 15px;
  left: 0px;
  width: 15px;
  height: 100px;
  background-color:  green;
  color: yellow;
}
#crossY {
  position: absolute;
  top: 0px;
  left: 15px;
  width: 100px;
  height: 15px;
  background-color:  yellow;
  color: white;
}
#black {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100px;
  height: 100px;
  border: 15px solid black;
  z-index: 10;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="con">
  <div id="one"></div>
  <div id="crossX"></div>
  <div id="crossY"></div>
  <div id="black"></div>
</div>

【问题讨论】:

    标签: javascript jquery jquery-animate


    【解决方案1】:

    通过以下解决方案,可以保证“鼠标离开部分”仅在“鼠标进入部分”被填充后运行(反之亦然)。

    此外,该脚本还负责处理快速用户操作时的情况:“进入 > 离开 > 进入”状态保持就像用户没有完成“快速离开”一样。 所以实际上这应该可以达到你想要达到的效果(至少我希望如此)。

    var mouseEnter = function() {
          // console.log('in');
          sPosition = 'in';
          if ( !mouseEnterIsDone || !mouseLeaveIsDone ) return mouseEnterIsWaiting = true;
          mouseEnterIsDone = false;
          $('#crossX').animate({'width':'115px'}, function(){
            $.when($('#crossY').animate({'height': '115px'})).then(function(){sanitizeAnimation('enter')})
          })
        },
        mouseLeave = function() {
          // console.log('out');
          sPosition = 'out';
          if ( !mouseEnterIsDone || !mouseLeaveIsDone ) return mouseLeaveIsWaiting = true;
          mouseLeaveIsDone = false;
          $('#crossY').animate({'height':'15px'}, function(){
            $.when($('#crossX').animate({'width': '15px'})).then(function(){sanitizeAnimation('leave')})
          })
        },
        sanitizeAnimation = function( sMode ){
          if ( 'enter' == sMode )
              mouseEnterIsDone = true;
          else
              mouseLeaveIsDone = true;
          if ( 'in' == sPosition ) {
            if ( mouseEnterIsWaiting ) {
              mouseEnterIsWaiting = false;
              mouseEnter();
            }
          } else {
            if ( mouseLeaveIsWaiting ) {
              mouseLeaveIsWaiting = false;
              mouseLeave();
            }
          }
        },
        mouseEnterIsDone = true,
        mouseLeaveIsDone = true,
        mouseEnterIsWaiting = false,
        mouseLeaveIsWaiting = false,
        sPosition = 'out';
    
    $(document).ready(function(){
      $('#con').hover(mouseEnter, mouseLeave);
    });
    body {
      padding: 5%;
    }
    
    #con {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 130px;
      height: 130px;
      overflow: hidden;
      //background-color: black;
    }
    #one {
      width: 100px;
      height: 100px;
      background-color: lightgrey;
      color:black
    }
    #crossX {
      position: absolute;
      top: 15px;
      left: 0px;
      width: 15px;
      height: 100px;
      background-color:  green;
      color: yellow;
    }
    #crossY {
      position: absolute;
      top: 0px;
      left: 15px;
      width: 100px;
      height: 15px;
      background-color:  yellow;
      color: white;
    }
    #black {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100px;
      height: 100px;
      border: 15px solid black;
      z-index: 10;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="con">
      <div id="one"></div>
      <div id="crossX"></div>
      <div id="crossY"></div>
      <div id="black"></div>
    </div>

    如果您需要进一步的解释,请随时发表评论

    【讨论】:

    • 阿克塞尔,感谢您的回复和解释。您的解决方案使动画效果更好,并且确实解决了我提出的问题。我现在想知道是否有一种方法可以使动画在光标离开元素时从其离开的位置缩回。
    • 是的@DustinL_G,这确实应该是可能的。我建议阅读this answer of mine 以更好地了解使用 JS 或 CSS 制作的动画。如果您愿意,可以在后续问题上联系我...
    【解决方案2】:

    $("#con").mouseenter(function() {
    $('body').addClass('Hover');
         $('#crossX').stop().animate({'width':'115px'},500, function(){
         $('#crossY').stop().animate({'height': '115px'},500);
         });
          
    });
    
    $("body").mouseenter(function() {
    $('body').addClass('Hover');
       	  $('#crossY').stop().animate({'height':'0px'},500,function(){
          $('#crossX').stop().animate({'width':'0px'},500);
         });
          
    });
    #con {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 130px;
      height: 130px;
      overflow: hidden;
      //background-color: black;
    }
    #one {
      width: 100px;
      height: 100px;
      background-color: lightgrey;
      color:black
    }
    #crossX {
      position: absolute;
      top: 15px;
      left: 0px;
      width: 15px;
      height: 100px;
      background-color:  green;
      color: yellow;
    }
    #crossY {
      position: absolute;
      top: 0px;
      left: 15px;
      width: 100px;
      height: 15px;
      background-color:  yellow;
      color: white;
    }
    #black {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100px;
      height: 100px;
      border: 15px solid black;
      z-index: 10;
    }
    body{
      background-color:#dcdcdc;
      height:500px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <body>
    <div id="con">
      <div id="one"></div>
      <div id="crossX"></div>
      <div id="crossY"></div>
      <div id="black"></div>
    </div>
    </body>

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多