javascript 简单的淡入淡出图像交换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14795007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple fading image swap
提问by HerrimanCoder
I'm having trouble finding a simple jquery image swap. Most the examples I've found are more complex than I need, and do things I don't want.
我很难找到一个简单的 jquery 图像交换。我发现的大多数示例都比我需要的更复杂,并且会做我不想做的事情。
Objective: I have 5 images I want to fade in, slide in, or etc. I would love to fade/dissolve from one image to the next, but slide would be fine too. When the page 1st loads, I want the 1st image to show for 4 seconds...then fade to the next image, 4 seconds, then the next, etc. Infinite loop.
目标:我有 5 张图像要淡入、滑入等。我想从一个图像淡入/溶解到下一个图像,但幻灯片也可以。当第一个页面加载时,我希望第一个图像显示 4 秒......然后淡入下一个图像,4 秒,然后下一个,等等。无限循环。
Currently my code is a simple image swap, not very elegant:
目前我的代码是一个简单的图像交换,不是很优雅:
document.getElementById("imgMain").src = "images/yurt/sleigh.png";
What's the best and simplest way to accomplish this?
实现这一目标的最佳和最简单的方法是什么?
回答by Jeff Miller
Here's the code:
这是代码:
HTML
HTML
<div class="fadein">
<img src="http://farm9.staticflickr.com/8359/8450229021_9d660578b4_n.jpg">
<img src="http://farm9.staticflickr.com/8510/8452880627_0e673b24d8_n.jpg">
<img src="http://farm9.staticflickr.com/8108/8456552856_a843b7a5e1_n.jpg">
<img src="http://farm9.staticflickr.com/8230/8457936603_f2c8f48691_n.jpg">
<img src="http://farm9.staticflickr.com/8329/8447290659_02c4765928_n.jpg">
</div>
CSS
CSS
.fadein {
position:relative;
height:320px;
width:320px;
}
.fadein img {
position:absolute;
left:0;
top:0;
}
JavaScript
JavaScript
$('.fadein img:gt(0)').hide();
setInterval(function () {
$('.fadein :first-child').fadeOut()
.next('img')
.fadeIn()
.end()
.appendTo('.fadein');
}, 4000); // 4 seconds