简体   繁体   English

用javascript进行图像过渡

[英]image transition with javascript

Can anyone suggest a lightweight fading image transition for javascript or jquery. 任何人都可以为javascript或jquery推荐轻量级的褪色图像过渡。 I'm thinking something along the lines of this: 我在想一些类似的事情:

document.getElementById("foo").src="images/robin.jpg";

where the html looks like this: html看起来像这样:

<img id="foo" src="images/batman.png"/>

The problem here is that the image doesn't fade, rather changes immediately. 这里的问题是图像不会褪色,而是立即改变。 I can certainly stack and animate two images independently, but I'm trying to replace one image. 我当然可以分别堆叠和设置两个图像的动画,但是我正在尝试替换一个图像。 Just makes for tidier code. 只是使整理代码。 Any advice is much appreciated. 任何建议深表感谢。 Thanks! 谢谢!

How about doing it with pure CSS3? 用纯CSS3怎么做? Use transition, opacity and z-index which will get you this effect with smooth effect 使用transition, opacityz-index ,可以使您获得具有平滑效果的效果

Demo 演示版

CSS 的CSS

div.wrap {
    position: relative;
    display: inline-block;
}

div.wrap img {
    position: absolute;
}

div.wrap img:nth-of-type(1) {
    opacity: 0;
    transition: opacity 3s;
}

div.wrap:hover img:nth-of-type(1) {
    z-index: 2;
    opacity: 1;
}

div.wrap img:nth-of-type(2) {
    opacity: 1;
    transition: opacity 3s;
}

div.wrap:hover img:nth-of-type(2) {
    opacity: 0;
}

Update 2 : Initially I used z-index but I don't think we require that too.. 更新2:最初我使用z-index但我认为我们也不需要。

Demo 2 (Without z-index ) 演示2 (无z-index

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

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