[英]Fade In/Fade Out Through Unknown Amount of Divs, With Delay Between
I've search around and tried my best, but could not find out how to achieve the following: 我四处搜寻并尽力而为,但无法找到如何实现以下目标:
My Current code is as follows 我的当前代码如下
JS Fiddle Link - Example JS Fiddle Link - 示例
HTML HTML
<div class="container">
<div class="popup">Popup 1</div>
<div class="popup r">Popup 2</div>
<div class="popup b">Popup 3</div>
<div class="popup g">Popup 4</div>
<div class="popup y">Popup 5</div>
</div>
CSS CSS
.popup {
display: none;
width: 400px;
height: 80px;
background: #000;
color: #fff;
line-height: 80px;
text-align: center;
}
.r{background:red}
.b{background:blue}
.g{background:green}
.y{background:yellow}
jQuery jQuery的
var popups = $('.popup');
var i = 0;
function step() {
if (i >= popups.length)
i = 0;
$(popups[i]).fadeToggle(300);
i++;
}
setInterval(step, 2000);
As you can see, my divs don't fade out until all are shown, this is not desired. 正如你所看到的,我的div在显示之前不会淡出,这是不可取的。
You can chain together animations with a delay
between: 您可以将动画链接在一起,
delay
时间:
function next() {
if (i >= popups.length)
i = 0;
popups.eq(i).fadeIn(300).delay(2500).fadeOut(300).delay(1000).queue(function(){
next();
$(this).dequeue();
});
i++;
}
next()
(note: I have used popups.eq(i)
which is the same as $(popups[i])
) (注意:我使用的
popups.eq(i)
与$(popups[i])
)
Live example: http://jsfiddle.net/3ujb7k4L/7/ 实例: http : //jsfiddle.net/3ujb7k4L/7/
Something like this? 像这样的东西?
var popups = $('.popup');
var i = 0;
function fadeOutLater(popup) {
setInterval(function() {
popup.fadeOut(500);
}, 5000);
}
function step() {
var popup;
if (i >= popups.length)
i = 0;
popup = $(popups[i]);
popup.fadeIn(300, fadeOutLater(popup));
i++;
}
setInterval(step, 2000);
To create a longer pause between hiding the last element and showing the first element again you could skip one fadeIn/fadeOut cycle on resetting the counter: 要在隐藏最后一个元素和再次显示第一个元素之间创建一个更长的暂停,您可以在重置计数器时跳过一个fadeIn / fadeOut循环:
var popups = $('.popup');
var i = 0;
function step() {
if (i >= popups.length) {
i = 0;
} else {
$(popups[i]).delay(300).fadeIn(300).delay(200).fadeOut(300).delay(200);
i++;
}
}
setInterval(step, 2000);
Like here: http://jsfiddle.net/r72qpher/ 喜欢这里: http : //jsfiddle.net/r72qpher/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.