0
I have a function and want to adapt it to work without needing the element ID (document.getElementById("slides");
). I saw that there is the possibility to do this using the operator NEW
but I had difficulties. Have some practical example that can help?
pg = 0;
sg = 20000;
ft = document.getElementById("slides");
sn = ft.getElementsByTagName("div");
tt = sn.length-1;
st = ["content-slide show-slide", "content-slide"];
//onclick
function slide(i){"proximo"==i&&md_n(),"anterior"==i&&md_p()}
//próximo
function md_n(){
if(pg < tt){
pg=pg+1;
sn.item(pg).setAttribute("class", st[0]);
if(pg > 0){
sn.item(pg-1).setAttribute("class", st[1]);
}
}else if(pg > 0 && pg == tt){
pg=0;
sn.item(pg).setAttribute("class", st[0]);
sn.item(tt).setAttribute("class", st[1]);
}
console.log("slide: "+pg);
}
//anterior
function md_p(){
if(pg > 0){
pg=pg-1;
sn.item(pg).setAttribute("class", st[0]);
if(pg > -1){
sn.item(pg+1).setAttribute("class", st[1]);
}
}else if(pg > -1 && pg != tt){
pg=sn.length-1;
sn.item(pg).setAttribute("class", st[0]);
sn.item(pg-(tt)).setAttribute("class", st[1]);
}
console.log("slide: "+pg);
}
//play
start = setInterval(function(){md_n()}, sg);
The problem with me selecting the elements for the class is that this way it would create conflict if I tried to implement more than one slide per page
– Odair
Edited above, you can give the attributes you want, in this case use querySelectorAll(), as shown above. This way you can get all the slides from the page. I explained below how it works
– Miguel
Perfect the Migrate explanation, but note that when implementing two "Slides" on the page it is not possible for me to use the same function, as in the following example: -> https://jsbin.com/zihosivexo/edit?html,js output,
– Odair
You need to cycle through all slides: http://jsfiddle.net/4huzr/2676/ . Try to implode like this at the beginning of js
– Miguel
Even so, it still hasn’t worked: https://jsbin.com/zihosivexo/edit?html,js,output
– Odair