I have the following JS
window.onload = function() {
var indexAtiva = 0;
const lis = document.getElementsByClassName('slider').item(0).getElementsByTagName('li');
function slider() {
for (i = 0; i < lis.length; i++) {
if (i != indexAtiva) {
lis[i].classList.remove('ativa');
} else {
lis[i].className = 'ativa'
}
}
if ((indexAtiva + 1) == lis.length) {
indexAtiva = 0;
} else {
indexAtiva++;
}
setTimeout(slider, 3000);
}
slider();
var slider = document.getElementsByClassName('slider')[0]
var nav = slider.getElementsByTagName('nav')[0]
var anterior = nav.getElementsByClassName('anterior')[0]
var proximo = nav.getElementsByClassName('proximo')[0]
anterior.onclick = function(){
prev = indexAtiva - 1
prev = prev.length ? prev : lis[ lis.length - 1 ];
mostraBloco(prev);
}
proximo.onclick = function(){
next = indexAtiva + 1
next = next.length ? next : lis[0];
mostraBloco(next);
}
// Função para exibir as imagens
function mostraBloco(next) {
ativa = document.getElementsByClassName('ativa')
ativa[0].classList.remove('ativa')
next.className = 'ativa'
}
}
* {
margin:0;
padding:0;
border:none;
outline:0;
}
body {
width:100vw;
}
ul {
list-style: none;
}
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes slider {
0% {
transform: scale(1);
}
100% {
transform: scale(1.1);
}
}
div.slider {
position:relative;
width:100%;
overflow:hidden;
}
div.slider ul.slide {
}
div.slider ul.slide li{
display:none;
}
.ativa{
display:block !important;
}
div.slider ul.slide li img {
position:relative;
width: 100%;
animation: slider 1s linear;
animation-fill-mode: forwards;
}
div.slider ul.slide li span {
position:absolute;
width: 100px;
left: calc(50% - 50px);
line-height:40px;
bottom:0;
text-align:center;
color:rgb(255,255,255);
z-index:2;
}
div.slider nav {
position:absolute;
width:100%;
height:40px;
bottom:0;
background-color:rgba(0,0,0,.5);
z-index:1;
}
div.slider nav button {
position:absolute;
width:150px;
height:100%;
cursor:pointer;
}
div.slider nav button.anterior {
left: 10%;
}
div.slider nav button.proximo {
right: 10%;
}
<div class="slider">
<ul class="slide">
<li class="ativa">
<img class="fade" src="_img/_banner/_site/bg_1.jpg" />
<span>Este é 1</span>
</li>
<li>
<img class="fade" src="_img/_banner/_site/bg_2.jpg" />
<span>Este é 2</span>
</li>
</ul>
<nav>
<button class="anterior">Anterior</button>
<button class="proximo">Próximo</button>
</nav>
</div>
My goal is to get a UL
with your Lis
and show it one by one.
But not all at once.
The error is as follows: the slide runs normally. But when it arrives at the end, it crashes and the console does not show the error.
Where is the error?
Note: If I remove the part that is below
slider();
All, then the slider runs normal. That is, the problem must be in the navigation buttons
Follow the online link