【问题标题】:How to make a gif animate on hover, go back to normal after unhovered如何在悬停时制作gif动画,悬停后恢复正常
【发布时间】:2016-08-15 06:34:57
【问题描述】:

所以我有一堆元素,例如:

<div class="hover-icon one">
    <img class="original" src="sswe-images/Circle_Customer Notifications.png"/>
    <img class="hovered one" src="sswe-images/Customer-Notifications.gif" />
</div>
<div class="hover-icon two">
    <img class="original" src="sswe-images/Circle_Field Service Tools.png"  />
    <img class="hovered" src="sswe-images/Field-Service-Tools.gif" />
</div>
<div class="hover-icon three">
    <img class="original" src="sswe-images/Circle_Remote Connectivity.png"  />
    <img class="hovered" src="sswe-images/Remote-Connectivity.gif" />
</div>

.original 是占位符,.hovered 是我想在悬停时制作动画的 gif,然后在鼠标离开后恢复正常状态。我的尝试是:

$('div.hover-icon').hover(function(){
    var orig = $(this).find('.original');
    orig.hide();
    var hov = $(this).find('.hovered');
    hov.attr('src', hov.attr('src') + "?x=" + Math.random());
    setTimeout(function(){ hov.show(); }, 100);        
    /* $(this).mouseleave(function(){
        hov.hide();
        orig.show();
    });*/
});

但注释掉部分的原因是它不起作用。它引起了各种疯狂。我应该在这里使用什么正确的模式?

【问题讨论】:

  • 如果你想支持 CSS3,可能有更简洁的方法来做到这一点。 Google 搜索“css gif 动画开始停止”会发现很多可能性。
  • 最简单的解决方案是有一个简单的图像,并在悬停时将其替换为 GIF 图像。

标签: javascript jquery html css gif


【解决方案1】:

基本的 HTML 结构是正确的。只使用 CSS,就像这个 codepen http://codepen.io/ryanpcmcquen/pen/gGqdI 一样

.hover-icon {
  position: relative;
  margin: 0 auto;
}

.hover-icon img {
  position: absolute;  
}

.hover-icon img.original:hover {
  opacity: 0;
}

【讨论】:

猜你喜欢
  • 2019-09-01
  • 2011-11-20
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2020-11-21
  • 2022-10-04
  • 1970-01-01
相关资源
最近更新 更多