简体   繁体   English

onmouseover 平滑地改变图像

[英]onmouseover change image smoothly

I have an image switching with another one when hovering with mouse, it works well, but I want to make it change smoothly.用鼠标悬停时,我有一个图像与另一个图像切换,效果很好,但我想让它平滑变化。

Here's my code :这是我的代码:

<img src="/images/doubleimgindex.png" class="w-100" 
     onmouseover="this.src='/images/doubleimgindex2.png'" 
     onmouseleave="this.src='/images/doubleimgindex.png'" />

I can't use div for this purpose, that's why I'm asking for help, is it possible?我不能为此目的使用 div,这就是我寻求帮助的原因,这可能吗?

If you want to fade between the images, you need to have two images on top of each other and fadein/out the upper one:如果要在图像之间淡入淡出,则需要将两张图像相互叠加并淡入/淡出上一张:

 .container { position: relative; } .upper-image { position: absolute; top: 0; left: 0; transition: opacity 1s; opacity: 0; } .upper-image:hover { opacity: 1; }
 <div class="container"> <img src="https://picsum.photos/id/100/400/300"> <img src="https://picsum.photos/id/200/400/300" class="upper-image"> </div>

In Action: https://codepen.io/theshark/pen/rNVVeLO在行动: https ://codepen.io/theshark/pen/rNVVeLO

The fading can be done completely in css as seen in the example :)褪色可以完全在 css 中完成,如示例中所示:)

The upper image is positioned on top of the lower image via position absolute and completely transparent.上图通过绝对位置和完全透明定位在下图之上。 On hover the image opacity is scaled to 1 and the image fades in.悬停时,图像不透明度缩放为 1,图像淡入。

If you have further questions about the code, please don't hesitate to ask :)如果您对代码还有其他问题,请随时提问:)

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

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