Lets build a really simple image slideshow using just Javascript. We will not use any jQuery or fancy libraries, just plain old JavaScript. This is for absolute beginners
Code (fiddle):
https://jsfiddle.net/bradtraversy/74owmd01/
Code (zip):
http://www.traversymedia.com/downloads/simpleslideshow.zip
My Courses:
http://traversymedia.com/eduonix-courses:
Donate – Any donation is greatly appreciated:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KLVYJVNUJ5NV4
Original source
44 responses to “Simple JavaScript Slideshow In 5 Minutes”
You are the bomb!
Thanks
Thank you so much! Great video!
let slide = document.slide
let i = 0
let images = []
let timeForNextImage = 3000
images[0] = 'img/image1.jpg'
images[1] = 'img/image2.jpg'
images[2] = 'img/image3.jpg'
images[3] = 'img/image4.jpg'
images[4] = 'img/image5.jpg'
function changeImageSlide() {
if( !images[i] )
i = 0
slide.src = images[i++]
time = setTimeout(changeImageSlide, timeForNextImage)
}
changeImageSlide()
can we put the images in the middle or change its length or breadth?
Setting name attribute and then calling it document.xxxx . ohhh i learned sth new today👍👍😎
change the "document.slide.src = image[i]" with "document.images.slide.src = image[i]"
This is so helpful. Thank you!!!!!!!!
Hey brad how can someone become a patron?
Richtig scheiss Video du hurensohn
Without html code run js editer name send me
I just donated $20 to u through PayPal. Thanks so much for the videos!!!
i = (i + 1) % images.length;
no need for the if
hey brad , can u show us how to do an automatic slideshow with dots indicator .
thank you internet end boss
thanks bro mine is working but can how to put link on those images
?and how can we add some buttons like 1 2 3 for each image so that user can see whatever image they want on the slide. else it will move automatically and that will not be user interactive. From that same process if it was possible then that would be more better! Anyways Great video
why does jpeg extension images doesn't load. do we need to install any encoder or decoder for that in JS?
really useful the code works perfectly fine ty very much
Hello!Why it does not work with for loop:
let images = ["gym2.jpg","gym3.jpg","gym4.jpg"];
function changeImg(){
var result;
for(let i = 0; i<images.length;i++){
result = images[i];
}
document.slide.src =result;
setTimeout("changeImg()",2000)
}
window.onload =changeImg;
Really you are a good teacher.Thanks a lot for your so easy & simple tutorials.
hello??
I'd like to have the next and previous button on my slides. Please have another 5 minutes video based on that. Thanks @Traversy Media.
You're literally my hero
Thank u so much , your tutorial helped me a lot
u have another subscriber 😁
thanks your code is great it helped me
hi man first thank you but that not a slide
this is a diaporama
thanks but it doesnt work
how to center slideshow?
nice one sir thanks
Thanks a lot
thanks Brad!
Hi Traversy Media i really like your code. if i wanted yo increase the duration of the slide
or to add a fade effect to the transition how will i do it?
Can someone explain why my array is undefined. I copied this perfectly. I get undefined or a blank screen for nearly everything I try to create. Every once in a while I can delete then re-add the exact same thing only to have it "magically work".
I did everything same and save images in the same folder as my HTML file but it dosen't run I get a box on briwser but images doesn't show
WUNDERBAR! You've got my LIKE, mann! The best video about how to make a slideshow with eficiency and few code.
Super – Danke – Liebe Grüße aus Innsbruck (Österreich)
images dont show up..need help.
Beautiful!!!
which code editor do you use….
What's the difference between:
setTimeout(changeImg, time); // and
setTimeout("changeImg()", time);
Is if you want to include () in callback function, you enclose them in quotation marks?
how do i style this tag?
dude, great vid but when i try to move it out of my navigation bar(cuz that where it spawned) it doesnt work
Excellent and straight to the point!
the slideshow worked but the images didnt show up
How is the looping happening? I can't see it in the code
can u do a tutorial on changing the slideshow into a video