Advertisement
victorumeh

news website wireframe

Feb 14th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const slider = document.querySelector(".slider");
  2. const nextBtn = document.querySelector(".next-btn");
  3. const prevBtn = document.querySelector(".prev-btn");
  4. const slides = document.querySelectorAll(".slide");
  5. const slideIcons = document.querySelectorAll(".slide-icon");
  6. const numberOfSlides = slides.length;
  7. var slideNumber = 0;
  8.  
  9. //image slider next button
  10. nextBtn.addEventListener("click", () => {
  11.   slides.forEach((slide) => {
  12.     slide.classList.remove("active");
  13.   });
  14.   slideIcons.forEach((slideIcon) => {
  15.     slideIcon.classList.remove("active");
  16.   });
  17.  
  18.   slideNumber++;
  19.  
  20.   if(slideNumber > (numberOfSlides - 1)){
  21.     slideNumber = 0;
  22.   }
  23.  
  24.   slides[slideNumber].classList.add("active");
  25.   slideIcons[slideNumber].classList.add("active");
  26. });
  27.  
  28. //image slider previous button
  29. prevBtn.addEventListener("click", () => {
  30.   slides.forEach((slide) => {
  31.     slide.classList.remove("active");
  32.   });
  33.   slideIcons.forEach((slideIcon) => {
  34.     slideIcon.classList.remove("active");
  35.   });
  36.  
  37.   slideNumber--;
  38.  
  39.   if(slideNumber < 0){
  40.     slideNumber = numberOfSlides - 1;
  41.   }
  42.  
  43.   slides[slideNumber].classList.add("active");
  44.   slideIcons[slideNumber].classList.add("active");
  45. });
  46.  
  47. //image slider autoplay
  48. var playSlider;
  49.  
  50. var repeater = () => {
  51.   playSlider = setInterval(function(){
  52.     slides.forEach((slide) => {
  53.       slide.classList.remove("active");
  54.     });
  55.     slideIcons.forEach((slideIcon) => {
  56.       slideIcon.classList.remove("active");
  57.     });
  58.  
  59.     slideNumber++;
  60.  
  61.     if(slideNumber > (numberOfSlides - 1)){
  62.       slideNumber = 0;
  63.     }
  64.  
  65.     slides[slideNumber].classList.add("active");
  66.     slideIcons[slideNumber].classList.add("active");
  67.   }, 4000);
  68. }
  69. repeater();
  70.  
  71. //stop the image slider autoplay on mouseover
  72. slider.addEventListener("mouseover", () => {
  73.   clearInterval(playSlider);
  74. });
  75.  
  76. //start the image slider autoplay again on mouseout
  77. slider.addEventListener("mouseout", () => {
  78.   repeater();
  79. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement