javascript - jQuery 动画悬停问题

标签 javascript jquery html hover

我有 3 个图像的以下代码,以便当用户将鼠标悬停在图像上时,图像会稍微缩放:

<script>
    jQuery(document).ready(function() {
        jQuery('#global-banner-cp-39c170794c8a8dfc143d053961f61ce3 img').hover(function() {
            jQuery(this).animate({width: '+=25px'}, {duration: 250, queue: false});
            jQuery(this).animate({height: '+=25px'}, {duration: 250, queue: false});
        }, function() {
            jQuery(this).animate({width: '-=25px'}, {duration: 250, queue: false});
            jQuery(this).animate({height: '-=25px'}, {duration: 250, queue: false});
        });

        jQuery('#global-banner-cp-a4fa4c6ccba117d459b3c5ef05c6642d img').hover(function() {
            jQuery(this).animate({width: '+=25px'}, {duration: 250, queue: false});
            jQuery(this).animate({height: '+=25px'}, {duration: 250, queue: false});
        }, function() {
            jQuery(this).animate({width: '-=25px'}, {duration: 250, queue: false});
            jQuery(this).animate({height: '-=25px'}, {duration: 250, queue: false});
        });

        jQuery('#global-banner-cp-3de99313614c53ea391132eca046e29c img').hover(function() {
            jQuery(this).animate({width: '+=25px'}, {duration: 250, queue: false});
            jQuery(this).animate({height: '+=25px'}, {duration: 250, queue: false});
        }, function() {
            jQuery(this).animate({width: '-=25px'}, {duration: 250, queue: false});
            jQuery(this).animate({height: '-=25px'}, {duration: 250, queue: false});
        });
    });
</script>

可以在此处查看演示:

https://ts361831-container.zoeysite.com/#global-wrapper-cp-09977e207fd109223f695752160117fd

如果您缓慢执行悬停,这效果很好,但如果您快速悬停或一次悬停在多个图像上,宽度和高度会不断减小。

任何人都可以帮我确定为什么会发生这种情况吗?预先非常感谢您。

最佳答案

试试这个!只需使用 css 变换即可实现

.image-box{
  width:300px;
  overflow:hidden;
}
.image {
	width:300px;
  height:200px;
	background-position:center;
	transition: all 1s ease;
  -moz-transition: all 1s ease;
  -ms-transition: all 1s ease;
  -webkit-transition: all 1s ease;
  -o-transition: all 1s ease;
} 
.image:hover {
  transform: scale(1.5);
  -moz-transform: scale(1.5);
  -webkit-transform: scale(1.5);
  -o-transform: scale(1.5);
  -ms-transform: scale(1.5); /* IE 9 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand')"; /* IE8 */
   filter: progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand'); /* IE6 and 7 */ 
} 
<div class="image-box">
  <img src="https://cdna4.zoeysite.com/Adzpo594RQGDpLcjBynL1z/cache=expiry:31536000/quality=v:70/compress/https://s3.amazonaws.com/zcom-media/sites/a0i0L00000QylPjQAJ/media/mediamanager/engineered.jpg" class="image">
</div>

<div class="image-box">
  <img src="https://cdna4.zoeysite.com/Adzpo594RQGDpLcjBynL1z/cache=expiry:31536000/quality=v:70/compress/https://s3.amazonaws.com/zcom-media/sites/a0i0L00000QylPjQAJ/media/mediamanager/built_to_last.jpg" class="image">
</div>

JSFIDDLE

关于javascript - jQuery 动画悬停问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42557670/

相关文章:

javascript - 每次值变化时运行一个方法

javascript - 将所有文本输入值添加到一个总量框中

javascript - 如何显示 JQPLOT 图而不是长文本

javascript - 为什么我收到 $ 未定义错误?

javascript - v-if 有两个条件

javascript - 打印机是否可以打印所有 cmyk 色域颜色以及 icc 配置文件的具体用途是什么?

javascript - 结合几个事件

jquery - Bootstrap 阻止 jQuery 更改复选框的状态

javascript - 如何在 getMilliSeconds Javascript 方法中将 0 转换为 000?

javascript - 如何将图像和数据添加到 firebase