JavaScript: W3.CSS Slideshow. What's it doing?
Hi,
I found this automatic slideshow that I want to use but I can't quite understand what is it doing: Code:
var slideIndex = 0; |
It is a standard loop you'll find in most programming languages
i is a counter variable, it starts at zero value; its value must be less than x.length value; i value is incremented by 1 for each loop iteration |
Why can't i be x.length? I would think that if the next slide will be 1 then the last one has to be set to "none"?
|
x.length is the length of x array, this array contains references to all elements that have .mySlides css classes in your html document
The loop sets css display property to none in each .mySlides css class The array index starts at zero and ends at array.length -1, that's why i (= the array index) can't be greater than x.length |
So if I have 5 elements, they will be 0, 1, 2, 3, and 4 in this loop?
|
the index i would have 0, 1, 2, 3, and 4 value in the loop, yes
the html elements containing .mySlides css class would be x[0], x[1] etc |
Thanks.
And why would I need i++, though? i = 0 takes care of 0 and x < x.length takes care of 1, 2, 3, and 4 in the case of 5 elements. It seems superfluous. |
|
The loop initializes all display attributes (style.display) of the mySlides class to none. (ie. hidden).
Since the scope of slideIndex is outside the function, it keeps the old value. So you need to increment it separately from "i" which initializes. As a further exercise, consider: Having initially initialized all displays to none, you don't really need to reinitialize all in every call to the function. You could have a variable called as "j" which holds the value of slideIndex before incrementing. Then you might change the code as: Quote:
OK |
Thanks all for your help. I'll get this someday.
Solved |
All times are GMT -5. The time now is 10:21 PM. |