I changed your code a bit to avoid code repetition, but basically what was happening was that in the troca
function you displayed the slide and then selected the next slide, but in next
and anterior
you first selecting the slide to display and then displaying it.
So what happened was that you displayed slide 3 and marked the next one as 1, but when the user clicked to display the next one, you selected the next one to be 2 (why you increase% with% from 1 to 2 ) and displays it.
Below is a snippet with the code working.
var satual = 3;
var smax = 3;
var stmp = 3000;
var interval;
slider();
// funcao de iniciar o slider
function slider() {
exibeSlide(satual);
// chama uma funcao a cada tempo
interval = setInterval(troca, stmp);
}
function exibeSlide(index) {
document.getElementById("b1").style.visibility = "hidden";
document.getElementById("b2").style.visibility = "hidden";
document.getElementById("b3").style.visibility = "hidden";
// exibindo imagem com index
document.getElementById("b" + index).style.visibility = "visible";
}
function troca() {
satual = satual + 1;
if (satual > smax) {
satual = 1;
}
exibeSlide(satual);
}
function next() {
clearInterval(interval);
satual++;
if (satual > smax) {
satual = 1;
}
exibeSlide(satual);
interval = setInterval(troca, stmp);
}
function anterior() {
clearInterval(interval);
satual--;
if (satual < 1) {
satual = 3;
}
exibeSlide(satual);
interval = setInterval(troca, stmp);
}
<div id="b1"><p>1</p></div>
<div id="b2"><p>2</p></div>
<div id="b3"><p>3</p></div>
<a href="#" onclick="anterior()">Anterior</a>
<a href="#" onclick="next()">Próximo</a>