+ 2
Can somebody make me able to understand this code
var images = [ "http://www.sololearn.com/uploads/slider/1.jpg", "http://www.sololearn.com/uploads/slider/2.jpg", "http://www.sololearn.com/uploads/slider/3.jpg" ]; var num = 0; function next() { var slider = document.getElementById("slider"); num++; if(num >= images.length) { num = 0; } slider.src = images[num]; } function prev() { var slider = document.getElementById("slider"); num--; if(num < 0) { num = images.length-1; } slider.src = images[num]; }
3 Respuestas
+ 16
What do you not understand?
Arrays, functions, variables, working with DOM, objects, properties?
Basicaly, it's an image slider code.
If you call next(), this function takes the next image URL from the array and puts it in the property src of the DOM element with id "slider".
If the current image is the last one of the array, the first image URL is taken.
prev() works in the opposite way.
+ 4
tell us the specific thing which you didnt get...
if you didnt get even a single line..
then i will suggest you to start js from scratch...
+ 2
Kumar Sarthak hi,
I guess your html got an iframe or img element with id "slider" to contain pictures holded in the images array.
Also , you must have two buttons or kind of with attribute "onclick" to trigger events ( here your two functions next() and prev() ).
So in your js you have two variables : the picture array and "num" which initialize a counter that will be used in your functions and be incremented by +1 or -1 depending what function have been triggered.
Your two functions just act that the event is triggered , and make your pictures go back or forth thanks to the incremented num,
and value of src attribute is picked up from the array according to the num value.
var num is initialized in the global scope, because if it was inside the function,each time you click it will get its original value,so the same image.
Hope it helps a little.