我创建了一个小的自动滑块,它每4秒改变一次图像,所以我用setInterval(displayImage, 4000);
.
图像被完美地改变;当显示第一图像时,存在的第一个点(由OL表示)保持激活,而第二个点保持禁用,并且到目前为止是良好的。
当图像发生变化时,即显示第二个点时,两个点都保持禁用状态(而我希望禁用第一个点,激活第二个点)。
我哪里做错了?这是我的准则:
function displayImage() {
let nodeList = document.querySelectorAll(".sliderimg");
let pointer = document.querySelectorAll("ol.controlnav li a")
for (let i = 0; i < nodeList.length; i++) {
for (let j = 0; j < pointer.length; j++) {
if (nodeList[i].classList.contains("active")) {
nodeList[i].classList.remove("active");
pointer[j].classList.remove("controlactive");
}
nodeList[count].classList.add("active");
pointer[count].classList.add("controlactive")
}
}
count += 1;
if (count == nodeList.length) {
count = 0;
}
}
.containimg .sliderimg {
display: none;
}
.containimg .sliderimg.active.imageright {
display: flex;
animation: opac 0.8s;
}
.containimg .sliderimg.active.imageright2 {
display: flex;
animation: opac 0.8s;
}
.containimg .sliderimg.active.imageright3 {
display: block;
position: relative;
}
.imageright {
background-image: url('https://themewagon.github.io/jackson/images/img_bg_2.jpg');
background-size: cover;
background-position: center;
height: 100vh;
display: flex;
align-items: center;
position: relative;
}
.imageright2 {
background-image: url('https://themewagon.github.io/jackson/images/img_bg_1.jpg');
background-size: cover;
background-position: center;
height: 100vh;
display: none;
align-items: center;
position: relative;
}
.controlnav {
position: absolute;
text-align: center;
bottom: 20px;
z-index: 1000;
left: 20px;
width: auto;
margin: 0;
padding: 0;
list-style: none;
}
.controlnav li {
margin: 8px;
}
.controlpaging li a {
width: 12px;
height: 12px;
display: block;
cursor: pointer;
text-indent: -9999px;
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.3);
border-radius: 20px;
}
.controlpaging li a.controlactive {
background: rgba(147, 147, 147, 0.795);
}
<div class="containimg">
<div class="imageright sliderimg active">
<div class="textimage">
<h1>I am</h1>
<h2>a Designer</h2>
<h3>100% html5 bootstrap templates Made</h3>
<h3>by <a href="#" class="underline">colorlib.com</a></h3>
<button class="viewportfolio mt-4">view portfolio <i
class="fa-solid fa-suitcase"></i></button>
</div>
<ol class="controlnav controlpaging">
<li><a href="#" class="controlactive">1</a></li>
<li><a href="#">2</a></li>
</ol>
</div>
<div class="imageright2 sliderimg">
<div class="textimage">
<h1>Hi!</h1>
<h2>I'm Sarah</h2>
<h3>100% html5 bootstrap templates Made</h3>
<h3>by <a href="#" class="underline">colorlib.com</a></h3>
<button class="viewportfolio mt-4">view portfolio <i
class="fa-solid fa-suitcase"></i></button>
</div>
<ol class="controlnav controlpaging">
<li><a href="#">1</a></li>
<li><a href="#" class="controlactive">2</a></li>
</ol>
</div>
</div>
有谁能帮帮我吗?
1条答案
按热度按时间j8ag8udp1#
本质上,问题是您得到了两个独立的Nodes集合,但使用相同的索引来解除对它们的引用。
.sliderimg
获取两个div
元素,但ol.controlnav li a
获取四个a
元素。当前代码合并了这两个独立的集合。如果您对如何查询元素有更具体的要求,则可以使用相同的索引,并具有更强的控制力: