循环访问一系列 DIV 并淡入/淡出
Iterate through an array of DIVs and fadeIn/fadeOut
我想构建一个简单的横幅图像滑块功能,但图像应该淡入淡出,而不是滑动。
这是一个Drupal项目,所以我不知道将使用多少图像。
结构很简单:
<div class="banner-container">
<!-- potentially a lot of divs with background images -->
</div>
我想做的本质上是遍历这个div 的"数组",让当前活动的div 淡出,让数组目前指向的那个淡入。
我想我可以让它像这样工作:
var a = $('#banner-container').children();
while(1) {
a.each(function(){
$('.active').fadeOut(500).removeClass('active');
$(this).fadeIn(500).addClass('.active');
})
}
显然,我的代码中有很多错误,我遇到了多个问题,但我似乎无法解决的是让循环在迭代之间等待。据我所知,像foreach
这样的传统循环可以在它的末尾添加某种wait
函数,并在每个循环中执行该wait
。但是.each()
似乎同时执行每个迭代。我怎样才能遍历这样的数组(可能是无限的)并每次等待 5 秒?
我在某处读到animate
函数异步工作,我认为 CSS 转换也是如此,因此创建 fadeIn
和fadeOut
类并使用 CSS 尝试此操作是行不通的。
您正在寻找setTimeout
和setInterval
函数:您将一个超时(以毫秒为单位)的函数传入它们,它们在该毫秒后调用该函数(在setInterval
的情况下重复)。
例如:
var a = $('#banner-container').children();
var index = 0;
run();
function run() {
a.filter('.active').fadeOut(500).removeClass('active');
a.eq(index).fadeIn(500).addClass('active');
index = (index + 1) % a.length; // Wraps around if it hits the end
setTimeout(run, 1000);
}
这将更新第一个div,然后在第二个之后更新下一个div,依此类推。
现场示例:
// Scoping function to avoid creating globals
(function() {
var a = $('#banner-container').children();
var index = 0;
run()
function run() {
a.filter('.active').fadeOut(500).removeClass('active');
a.eq(index).fadeIn(500).addClass('active');
index = (index + 1) % a.length; // Wraps around if it hits the end
setTimeout(run, 1000);
}
})();
#banner-container a {
display: none;
position: absolute;
}
#banner-container {
position: relative;
}
<div id="banner-container">
<a>one</a>
<a>two</a>
<a>three</a>
<a>four</a>
<a>five</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
在上面,我使用了setTimeout
,它只安排一个回调,因为我发现当我调用的东西必须显式重新安排下一次调用时,我写的错误更少。但是您也可以使用setInterval(run, 1000);
,它将以大约一秒的间隔调用run
,直到/除非您通过clearInterval
告诉它停止。
由于JavaScript的性质,你不能为此使用循环,你将不得不使用递归,像这样未经测试的代码应该可以工作。
function loopNext(currentIndex){
// set the current index on the first call
if('undefined' === typeof currentIndex) currentIndex=0;
// the group of image divs
var images = $(".imagesDivs");
// if the current index doesn't exist,
// we've looped thru them all, reset the index
if('undefined' === typeof images.eq(currentIndex)) currentIndex=0;
// fade out the previous one and fade in the first one
images.fadeOut('slow', function(){
images.eq(currentIndex).fadeIn('slow');
});
// fade the next one every 3 seconds or so
setTimeout(function(){
loopNext(currentIndex+1);
}, 3000);
}
要启动循环,您可以在没有任何参数的情况下调用它。
loopNext();
您可以使用setTimout并转到下一个元素或重新开始:
setTimout(function(){
var current = $('.active');
var next = current.next().length > 0 ? current.next() : $('.banner-container > div').first();
current.fadeOut(500).removeClass('active');
next.fadeIn(500).addClass('.active');
},5000);
- Javascript将图像src更改为淡入淡出
- css(或jQuery)悬停时淡入淡出
- 将淡入淡出添加到“我的身体背景滑块”
- 多重潜水淡入淡出打开
- 添加/删除类淡入淡出不起作用
- 带有计数器、缩略图、进度条和淡入淡出效果的简单jQuery幻灯片
- 使用jquery淡入淡出
- 淡入淡出脚本不起作用
- 具有淡入淡出效果的全背景图像
- jQuery滑块淡入淡出
- 在淡入淡出之间淡入淡出
- jQuery idTabs-使用鼠标悬停自动更改和淡入淡出
- 文本淡入淡出后循环加载页面
- 文本淡入淡出jquery
- 动画文本淡入淡出
- 如何添加淡入淡出效果
- (阅读更多链接)到模式淡入淡出视图
- 单击“淡入淡出”按钮
- JQuery刷新列表效果-淡入淡出问题
- 如何使部分在滚动中淡入淡出