I am trying to write an image slider in Javascript, I was looking at examples online and found this particular one:
function slider(){
var current = 0;
slides = document.getElementsByClassName("images");
setInterval(function() {
for (var i = 0; i < slides.length; i++) {
slides[i].style.opacity = 0;
}
current = (current != slides.length - 1) ? current + 1 : 0;
slides[current].style.opacity = 1;
}, 3000);
}
window.onload=slider;
I have tested this and it works with no errors.
I am wondering why the variable 'slides' is not declared as 'var slides', when I add 'var' the script stops working. Don't all variables in Javascript start with 'var'? why does this work without the 'var'?