javascript - jQuery onmouseenter 和 onmouseleave 在位置/大小更改动画时触发

标签 javascript jquery html css

我只知道 jQuery onmouseenter/onmouseleave每次元素的位置/大小在动画期间发生变化时,事件也会触发,因此我通过使用if (img$.is(':animated')) return false; 来进行简单的验证。在两个事件处理程序的开头。

但是,这会导致另一个问题,有时是 mouseleave事件永远不会因为 mouseleave 而被执行在 mouseenter 时提前返回 false还没完。

$(function() {

  $("img[data-alt-src]").on('mouseenter', function(e) {
      e.stopPropagation();
      e.preventDefault();
      var img$ = $(e.currentTarget);
      if (img$.is(':animated')) return false; // the validation
      img$.finish().animate({
        opacity: '-=1.0',
        deg: '+=90'
      }, {
        duration: 250,
        step: function(now) {
          img$.css({
            '-moz-transform': 'rotateY(' + now + 'deg)',
            '-webkit-transform': 'rotateY(' + now + 'deg)',
            '-o-transform': 'rotateY(' + now + 'deg)',
            '-ms-transform': 'rotateY(' + now + 'deg)',
            transform: 'rotateY(' + now + 'deg)'
          });
        },
        complete: function() {
          img$.data('tmp-src', img$.attr('src'));
          img$.attr('src', img$.data('alt-src'));
        }
      });
      img$.animate({
        opacity: '+=1.0',
        deg: '-=90'
      }, {
        duration: 250,
        step: function(now) {
          img$.css({
            '-moz-transform': 'rotateY(' + now + 'deg)',
            '-webkit-transform': 'rotateY(' + now + 'deg)',
            '-o-transform': 'rotateY(' + now + 'deg)',
            '-ms-transform': 'rotateY(' + now + 'deg)',
            transform: 'rotateY(' + now + 'deg)'
          });
        }
      });
    })
    .on('mouseleave', function(e) {
      e.stopPropagation();
      e.preventDefault();
      var img$ = $(e.currentTarget);
      if (img$.is(':animated')) return false; // the validation
      img$.finish().animate({
        opacity: '-=1.0',
        deg: '+=90'
      }, {
        duration: 250,
        step: function(now) {
          img$.css({
            '-moz-transform': 'rotateY(' + now + 'deg)',
            '-webkit-transform': 'rotateY(' + now + 'deg)',
            '-o-transform': 'rotateY(' + now + 'deg)',
            '-ms-transform': 'rotateY(' + now + 'deg)',
            transform: 'rotateY(' + now + 'deg)'
          });
        },
        complete: function() {
          img$.attr('src', img$.data('tmp-src'));
        }
      });
      img$.animate({
        opacity: '+=1.0',
        deg: '-=90'
      }, {
        duration: 250,
        step: function(now) {
          img$.css({
            '-moz-transform': 'rotateY(' + now + 'deg)',
            '-webkit-transform': 'rotateY(' + now + 'deg)',
            '-o-transform': 'rotateY(' + now + 'deg)',
            '-ms-transform': 'rotateY(' + now + 'deg)',
            transform: 'rotateY(' + now + 'deg)'
          });
        }
      });
    });

});
img {
  width: 150px;
  height: 150px;
  margin-right: 1.5em;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

  <div style="position: relative; display: inline-block">
    <img
      src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
      data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
      alt="">
    <img
      src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
      data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
      alt="">
    <img
      src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
      data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
      alt="">
    <img
      src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
      data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
      alt="">
  </div>
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  
</body>
</html>

enter image description here

我不知道如何摆脱这种情况?

最佳答案

我希望这会奏效...我添加了 setTimout 函数以在动画的一半时间内翻转图像源...

$(function() {
	$("img[data-alt-src]").on('mouseenter', function(e) {
		e.stopPropagation();
		e.preventDefault();
		var img$ = $(e.currentTarget);
		if (img$.is(':animated')) return false; // the validation
		img$.finish().animate({
			opacity: '-=1.0',
			deg: '+=90'
		}, {
			duration: 250,
			step: function(now) {
				img$.css({
					'-moz-transform': 'rotateY(' + now + 'deg)',
					'-webkit-transform': 'rotateY(' + now + 'deg)',
					'-o-transform': 'rotateY(' + now + 'deg)',
					'-ms-transform': 'rotateY(' + now + 'deg)',
					transform: 'rotateY(' + now + 'deg)'
				});
			}
		});
		setTimeout(function(){
			img$.data('tmp-src', img$.attr('src'));
			img$.attr('src', img$.data('alt-src'));
			img$.data('alt-src', img$.data('tmp-src'));
		}, 125);
		img$.animate({
			opacity: '+=1.0',
			deg: '-=90'
		}, {
			duration: 250,
			step: function(now) {
				img$.css({
					'-moz-transform': 'rotateY(' + now + 'deg)',
					'-webkit-transform': 'rotateY(' + now + 'deg)',
					'-o-transform': 'rotateY(' + now + 'deg)',
					'-ms-transform': 'rotateY(' + now + 'deg)',
					transform: 'rotateY(' + now + 'deg)'
				});
			}
		});
	  }).on('mouseleave', function(e) {
		e.stopPropagation();
		e.preventDefault();
		var img$ = $(e.currentTarget);
	      if (img$.is(':animated')) return false; // the validation
	      img$.finish().animate({
	      	opacity: '-=1.0',
	      	deg: '+=90'
	      }, {
	      	duration: 250,
	      	step: function(now) {
	      		img$.css({
	      			'-moz-transform': 'rotateY(' + now + 'deg)',
	      			'-webkit-transform': 'rotateY(' + now + 'deg)',
	      			'-o-transform': 'rotateY(' + now + 'deg)',
	      			'-ms-transform': 'rotateY(' + now + 'deg)',
	      			transform: 'rotateY(' + now + 'deg)'
	      		});
	      	}
	      });
	      setTimeout(function(){
	      	img$.data('tmp-src', img$.attr('src'));
	      	img$.attr('src', img$.data('alt-src'));
	      	img$.data('alt-src', img$.data('tmp-src'));
	      }, 125);
	      img$.animate({
	      	opacity: '+=1.0',
	      	deg: '-=90'
	      }, {
	      	duration: 250,
	      	step: function(now) {
	      		img$.css({
	      			'-moz-transform': 'rotateY(' + now + 'deg)',
	      			'-webkit-transform': 'rotateY(' + now + 'deg)',
	      			'-o-transform': 'rotateY(' + now + 'deg)',
	      			'-ms-transform': 'rotateY(' + now + 'deg)',
	      			transform: 'rotateY(' + now + 'deg)'
	      		});
	      	}
	      });
	  });
});
img {
  width: 150px;
  height: 150px;
  margin-right: 1.5em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="position: relative; display: inline-block">
	<img
	src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
	data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
	alt="">
	<img
	src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
	data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
	alt="">
	<img
	src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
	data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
	alt="">
	<img
	src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_1-270x270.jpg"
	data-alt-src="http://mcenter.lazim.org/image/cache/catalog/demo/nikon_d300_3-270x270.jpg"
	alt="">
</div>

关于javascript - jQuery onmouseenter 和 onmouseleave 在位置/大小更改动画时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56829748/

相关文章:

javascript - React 的 Firebase onAuthStateChanged 错误

javascript - 理解对象的属性+回调

javascript - 在悬停/单击时获取图像中颜色的十六进制代码?

jquery - 在 Visual Studio 中重新缩进(重新格式化)最小化 jquery/javascript 文件

jquery - 为什么隐藏 "select"选项?

javascript - 如何从 rgb/rgba 字符串中获取红绿蓝值?

html - Dart 混淆 TYPE 错误

javascript - 运行音频可以在桌面上运行,但不能在移动设备上运行

javascript - 如何找到浏览器窗口的 "maximized"高度?

html - 从页面中删除HTML5音频或视频时,如何防止播放?