简体   繁体   English

如何临时禁用元素的CSS悬停触发的动画?

[英]How can I temporarily disable an element's CSS hover-triggered animation?

Consider this slider: 考虑一下此滑块:

 $(function() { var $volumeBar = $('#Volume-Bar'); var $volumeContainer = $('#Volume-Container'); var $value = $('.value'); $volumeContainer.on('mousedown', function(event) { var height = $volumeContainer.height(); var top = $volumeContainer.position().top; var startingCoord = event.clientY - top; var currentCoord; var percent; var difference; seekingVol = true; $volumeBar.css({ 'background-color': '#dcb311', }); $volumeContainer.on('mouseup mouseleave', function() { if (seekingVol) { seekingVol = false; $volumeBar.css({ 'background-color': '', }); } }); $volumeContainer.on('mousemove', function(event) { if (seekingVol) { currentCoord = event.clientY - top; percent = ((height - currentCoord) / height) * 100; $volumeBar.finish(); $volumeBar.css({ 'height': percent + '%' }); } }); }); }); 
 @import url(https://fonts.googleapis.com/css?family=Bitter:700); #Volume-Container { position: relative; width: 75px; height: 150px; background-color: #0e2030; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; } #Volume-Bar { position: absolute; bottom: 0px; height: 30%; width: 100%; background-color: #6ab2f2; transform-origin: bottom; } #Volume-Bar:hover { background-color: #dcb311; animation: Vert-Bounce 0.6s; } @keyframes Vert-Bounce { 0% { transform: height(1); } 25% { transform: scaleY(1.3); } 50% { transform: scaleY(1); } 75% { transform: scaleY(1.15); } 100% { transform: scaleY(1); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="Volume-Container"> <div id="Volume-Bar"></div> </div> 

Notice the bounce animation on the bar on hover? 注意悬停时栏上的跳动动画吗?

When the user pulls the bar upward, the mouse exits the element due to the lag between the bar's scaling and the movement of the mouse. 当用户向上拉动条形图时,由于条形图的缩放和鼠标移动之间的滞后,鼠标会退出元素。 When the element catches up to the mouse or the user pulls back downward, another hover event is fired as the mouse re-enters the element. 当元素赶上鼠标或用户向后拉时,当鼠标重新进入元素时,会触发另一个悬停事件。 This triggers the bounce animation multiple times as the user drags the bar. 当用户拖动条时,这会多次触发跳动动画。 I want to disable this animation on mousedown and re-activate it on mouseup . 我想在mousedown上禁用此动画,并在mouseup上重新激活它。

How can I do this? 我怎样才能做到这一点?

If I understand well, the bouncing animation occurs on dragging. 如果我理解得很好,则在拖动时会出现弹跳动画。
And this is the problem. 这就是问题所在。

I tried something that may be of interest. 我尝试了一些有趣的事情。 I used mouseover and mouseout events in event listeners functions... Instead of the CSS hover to handle the bouncing effect. 我在事件侦听器函数中使用mouseovermouseout事件...而不是CSS hover来处理弹跳效果。

$(function() {

  var $volumeBar = $('#Volume-Bar');
  var $volumeContainer = $('#Volume-Container');
  var $value = $('.value');

  $volumeBar.on('mouseover', function(event) {      // Added this event function
    $volumeBar.css({"background-color":"#dcb311","animation":"Vert-Bounce 0.6s"});
  });

  $volumeBar.on('mouseout', function(event) {       // Added this event function
    $volumeBar.css({"background-color":"#6ab2f2","animation":"initial"});
  });

  $volumeContainer.on('mousedown', function(event) {
    var height = $volumeContainer.height();
    var top = $volumeContainer.position().top;
    var startingCoord = event.clientY - top;
    var currentCoord;
    var percent;
    var difference;
    seekingVol = true;
    $volumeBar.css({
      'background-color': '#dcb311',
    });
    $volumeContainer.on('mouseup mouseleave', function() {
      if (seekingVol) {
        seekingVol = false;
        $volumeBar.css({
          'background-color': '',
        });
      }
    });
    $volumeContainer.on('mousemove', function(event) {
      if (seekingVol) {
        currentCoord = event.clientY - top;
        percent = ((height - currentCoord) / height) * 100;
        $volumeBar.finish();
        $volumeBar.css({
          'height': percent + '%'
        });
      }
    });
  });
});

See Fiddle : https://jsfiddle.net/Bes7weB/8fpqxhoe/ 参见小提琴: https//jsfiddle.net/Bes7weB/8fpqxhoe/

NOTE: 注意:
I added a 6px padding to it: In order to prevent the mouse from mouseout on drag. 我在上面添加了6px的填充:为了防止鼠标在拖动时滑出鼠标。
This may be a problem... 可能是个问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM