[英]delay adding class to elements with setTimeout?
So I have a bunch of div elements that I want to add a class to, so it trigger a transition -- but I want the class to be added 1 at a time, not simultaneously so the animation appears 1 at a time. 因此,我有一堆div元素想要添加一个类,以便触发过渡-但我希望一次将类添加一次,而不是同时添加一次,因此动画一次出现1个。
This is how my javascript looks like. 这就是我的JavaScript的样子。
var campaignInfo = document.querySelectorAll(".campaign-info-container");
setTimeout(function(){
for(i=0; i<=campaignInfo.length;i++){
campaignInfo[i].classList.add("campaign-container-slide");
console.log(campaignInfo[i]);
};
}, 2000);
so basically I use querySelectorAll so it captures all the divs with that class, which shows up when I console.log()
it. 所以基本上我使用querySelectorAll,以便它捕获该类的所有div,当我
console.log()
时显示。
But regardless the classes are not added with a delay.. I'm not sure how I can add an iteration to the milliseconds 2000 * i
if i
is only defined inside the loops. 但是,不管类是否添加都没有延迟。.如果
i
仅在循环内部定义,我不确定如何将迭代添加到毫秒2000 * i
。 I tried creating a var counter;
我尝试创建一个
var counter;
outside the setTimeout
, and increment it inside the loop with counter++
, but the delay still won't work. 在
setTimeout
外部,并使用counter++
在循环内将其递增,但是延迟仍然不起作用。
Also, I'm getting this error "Can't read property classList of undefined. 另外,我收到此错误“无法读取未定义的属性classList。
Thanks guys. 多谢你们。 Oh and please no jQuery :)
哦,请不要使用jQuery :)
EDIT: 编辑:
Thanks for the solution! 感谢您的解决方案! :)
:)
So does the setInterval
work like a loop in your case? 那么在您的情况下,
setInterval
像循环一样工作?
Because I want this interval to occur whenever I click on a button, but removed when I exit out of the frame. 因为我希望每次单击按钮时都发生此间隔,但是当我退出框架时将其删除。 So I added the below to remove the class from the elements.
因此,我添加了以下内容以从元素中删除类。 But I use a
for
loop instead.. 但是我改用
for
循环。
for(i=0; i<=campaignInfo.length; i++){
campaignInfo[i].classList.remove("campaign-container-slide");
}
and this works, but I'm just curious how come my first initial code (not this one above) doesn't work? 并且可以,但是我很好奇为什么我的第一个初始代码(上面的代码不行)不起作用? how come using a counter works but not looping through it?
使用计数器如何工作却无法循环遍历?
If I understand you correctly, you could use nested setTimeout
calls, or use setInterval
: 如果我对您的理解正确,则可以使用嵌套的
setTimeout
调用,也可以使用setInterval
:
var campaignInfo = document.querySelectorAll(".campaign-info-container"); var index = 0; // The index of the next `campaignInfo` div to update var interval = setInterval(function() { // Create a new interval that fires every 500ms campaignInfo[index++].classList.add("campaign-container-slide"); if (index === campaignInfo.length) { // The last div has been reached, so... clearInterval(interval); // Clear the interval using the reference } }, 500);
body { font-size: 2em;} .campaign-info-container { background: #555; } .campaign-container-slide { background: #eee; }
<div class="campaign-info-container">One</div> <div class="campaign-info-container">Two</div> <div class="campaign-info-container">Three</div> <div class="campaign-info-container">Four</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.