简体   繁体   English

我想在活动幻灯片中添加“is-active” class 并在出现的幻灯片中添加“is-out” class

[英]I want to add the “is-active” class in the active slide and the “is-out” class in the slide that comes out

I have the following HTML document which is a SlideShow:我有以下 HTML 文档,它是幻灯片:

<div class="ReferencesCarousel">
  <div class="ReferencesCard"></div>
  <div class="ReferencesCard"></div>
  <div class="ReferencesCard"></div>
</div>

The javascript code relating to the code below is as follows:与下面代码相关的javascript代码如下:

var ReferenceIndex = 0;
showReferences();
function showReferences() {
 var i;
 var References = document.getElementsByClassName("ReferencesCard");
 for (i = 0; i < References.length; i++) {
  References[i].classList.remove('is-active');
 }
 ReferenceIndex++;
 if (ReferenceIndex > References.length) {ReferenceIndex = 1}
 References[ReferenceIndex-1].classList.add('is-active');
 setTimeout(showReferences, 2000);
}

What I want to do is add the "is-out" class in the slide that will come out.我想要做的是在将要出现的幻灯片中添加“is-out” class。

That means each class that contains "is-active" changes to "is-out" and the "is-out" class will be added to the next slide.这意味着每个包含“is-active”更改为“is-out”的 class 和“is-out” class 将添加到下一张幻灯片。

Another thing: the is-out class should last 1 second and it should be deleted.另一件事:已输出的 class 应该持续 1 秒,并且应该被删除。

You can use querySelector to find the node that has the.isActive class.您可以使用 querySelector 查找具有.isActive class 的节点。 Then you can remove it from the node where it occurs.然后您可以将其从它发生的节点中删除。

It will looking something like this:它看起来像这样:

let isActive = document.querySelector('.is-active')
if (isActive !== null){
isActive.classList.remove('is-active')

The setInterval method will take care of your timing issue. setInterval 方法将处理您的时间问题。 It will run a function after a set amount of time determined by you.它将在您确定的设定时间后运行 function。 Read more here. 在这里阅读更多。

Instead of running a for loop, you can set i = 0, like you did above.您可以像上面那样设置 i = 0,而不是运行 for 循环。 Then within your setInterval function, have:然后在你的 setInterval function 内,有:

i++
References[i].classList.add('is-active')

All of these together will 1) search for any nodes with the 'is-active' class 2)remove it, 3) add the 'is-active' class to the node in your list with index value equal to i in that interval.所有这些一起将 1) 搜索具有“活动” class 的任何节点 2) 删除它,3) 将“活动” class 添加到列表中的节点,该节点的索引值等于 i 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM