[英]How to make successively appear images in a blinking element?
I have been struggling with this for quite a time now. 我已经为此苦苦挣扎了很长时间了。
I have a div that i want to make appear every X seconds and make disappear every Y seconds. 我有一个div,我想使它每隔X秒出现一次,并使每隔Y秒消失一次。 No problem for that, I've got something like :
没问题,我有这样的东西:
jQuery : jQuery的:
function makeContainerAppear(displayTime, interval) {
setInterval( function() {
$("#container").show("slow","swing");
setTimeout( function() {
$("#container").hide("slow","swing");
}
, displayTime * 1000);
, interval * 1000);
}
$(document).ready(function() {
$("#container").hide();
makeContainerAppear(5,10);
});
Html : HTML:
<div id="container" class="forced-ad">
<div class="dummy"></div>
<div class="container-img centerer">
<img class="centered" src="http://placekitten.com/200/200">
</div>
</div>
And it works like a charm! 它就像一个魅力!
But i'm pretty annoyed when i want to make the same thing with multiples images :( 但是当我想用倍数图像制作相同的东西时我很生气:(
I want that each time my main #container
appear that the image in there change (and cycle). 我希望每次我的主要
#container
出现时,那里的图像都发生变化(并循环)。
I have thought of make something like : 我想到过做类似的事情:
<div id="container" class="forced-ad">
<div class="dummy"></div>
<div class="img-container centerer">
<img class="centered" src="http://placekitten.com/200/200" alt="forced-ad">
<img class="centered" src="http://placekitten.com/201/200" alt="forced-ad">
<img class="centered" src="http://placekitten.com/200/201" alt="forced-ad">
</div>
</div>
and 和
function makeContainerAppear(displayTime, interval) {
setInterval( function() {
$("#container").show("slow","swing");
$("img").hide();
$("img:eq(i)").show();
setTimeout( function() {
$("#container").hide("slow","swing");
}
, displayTime * 1000);
, interval * 1000);
}
$(document).ready(function() {
$("#container").hide();
makeContainerAppear(5,10);
});
But i can't find a way to increment my i
variable in the :eq(i)
line 5 and to make it cycle with the number of items ( img
) in the #container
div. 但是我找不到一种方法来增加我的
:eq(i)
第5行中的i
变量,并使其与#container
div中的项目数( img
)循环。
in this case make appear the image in that manner : 1 > 2 > 3 > 1 > 2 > 3 > ... 在这种情况下,以以下方式显示图像:1> 2> 3> 1> 2> 3> ...
Thanks for your suggestions ! 感谢您的建议!
you need to increment the var before you show the next img so it can be after the $("img:eq(i)").show(); 您需要在显示下一个img之前增加var,以便它可以在$(“ img:eq(i)”)。show()之后。 or inside the setTimeout( function() {
或在setTimeout(function(){
var i=0;//declare the i variable
function makeContainerAppear(displayTime, interval) {
setInterval( function() {
$("#container").show("slow","swing");
$("img").hide();
//$("img:eq("+i+")") or $("img").eq(i)
//i++ can be here
$("img:eq("+i+")").show();
setTimeout( function() {
//check if it is less than 3 return to 0 or use i%3 inside eq
i++;
$("#container").hide("slow","swing");
}
, displayTime * 1000);
}, interval * 1000);
}
http://jsfiddle.net/wxKx2/1/ http://jsfiddle.net/wxKx2/1/
or you can use .delay() like this 或者你可以像这样使用.delay()
//declare the adIndex variable and save the container and imgs elements
var adIndex=0,$container=$("#container"),$ads=$(".img-container").children("img");
function makeContainerAppear() {
//hide all $ads and only show the one in adIndex
$ads.hide().eq(adIndex%3).show();
$container.hide().delay(10000).show("slow","swing")
.delay(5000).hide("slow","swing",function(){
adIndex++;
//call the same function when container is hidden
makeContainerAppear();
});
}
makeContainerAppear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.