Created
May 27, 2017 03:08
-
-
Save hsipeng/6432b4983b7a8be248e95727ac475255 to your computer and use it in GitHub Desktop.
slider with autoplay function,using plain javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var slideIndex = 1; | |
| var t; | |
| shwoSlides(slideIndex); | |
| function pulsSlides(n) { | |
| shwoSlides(slideIndex +=n); | |
| } | |
| function currentSlide(n) { | |
| shwoSlides(slideIndex = n); | |
| } | |
| function shwoSlides(n) { | |
| var i; | |
| var slides = document.getElementsByClassName("mySlides"); | |
| var dots = document.getElementsByClassName("dot"); | |
| if(n>slides.length){ | |
| slideIndex = 1; | |
| } | |
| if(n<1){ | |
| slideIndex = slides.length; | |
| } | |
| for(i=0;i<slides.length;i++){ | |
| slides[i].style.display = "none"; | |
| } | |
| for(i = 0;i<dots.length;i++){ | |
| dots[i].className=dots[i].className.replace('active',''); | |
| } | |
| slides[slideIndex-1].style.display = "block"; | |
| dots[slideIndex-1].className+=" active"; | |
| } | |
| function autoPlay() { | |
| var i; | |
| var slides = document.getElementsByClassName("mySlides"); | |
| for (i = 0; i < slides.length; i++) { | |
| slides[i].style.display = "none"; | |
| } | |
| slideIndex++; | |
| if (slideIndex> slides.length) {slideIndex = 1} | |
| slides[slideIndex-1].style.display = "block"; | |
| t=setTimeout('autoPlay()', 2000); // Change image every 2 seconds | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment