show / hide image,text,video... | Sololearn: Learn to code for FREE!
0

show / hide image,text,video...

how to show one image , then hide it after 2 seconds , and then show another image ?

1st Oct 2018, 9:47 AM
Veljko Pejović
Veljko Pejović - avatar
1 Answer
+ 1
Do you mean something like an automatic slideshow? You can do that fro example by making giving the images the same class name: <img class="mySlides" src="pic1.jpg" style="width:100%"> <img class="mySlides" src="pic2.jpg" style="width:100%"> <img class="mySlides" src="pic3.jpg" style="width:100%"> And then create a for loop and add the settimeout method to switch images every two seconds for example: <script> var myIndex = 0; carousel(); function carousel() { var i; var x = document.getElementsByClassName("mySlides"); for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } myIndex++; if (myIndex > x.length) {myIndex = 1} x[myIndex-1].style.display = "block"; setTimeout(carousel, 5000); } </script>
8th Oct 2018, 9:18 AM
Wouter Tamminga