Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to pass setInterval() into a Javascript class method

const list = document.querySelector('.js-gallery');
const items = document.querySelectorAll('.gallery__item'); // Use this for slide count, not list
Array.from(list); // Doesn't do anything

class Gallery {
  constructor(slideshow) {
    this.slideshow = slideshow;
    this.slideCount = items.length;
    this.items = items[0].getBoundingClientRect().width;
    this.currentSlide = 1;
    this.slideTransitionInterval = null;
  }

  transitionSlide() {
    console.log('invoked');
    if (this.currentSlide < this.slideCount) {
      list.style.transform = `translateX(-${this.currentSlide * this.items}px)`;
      this.currentSlide += 1;
    } else {
      this.stopSlideshow();
      list.style.transform = `translateX(0)`;
      this.currentSlide = 1;
    }
  }

  // Trying to make a method with setInterval() so that the slide runs every 5 seconds.
  startSlideshow() {
    this.stopSlideshow();
    this.slideTransitionInterval = setInterval(this.transitionSlide.bind(this), 500);
  }
  
  stopSlideshow() {
    if (this.slideTransitionInterval) {
      clearInterval(this.slideTransitionInterval);
    }
  }

}

const pics = new Gallery(list);
pics.startSlideshow();
.title {
  width: 100%;
  text-align: center;
}

.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  padding: 0;
  transition: all 500ms ease;
}


.gallery-container {
  overflow: hidden;
  position: relative;
  width: 1000px;
  margin: 0 auto;
}

.gallery__item {
  list-style: none;
  height: 500px;
  min-width: 1000px;
  background-position: center center;
  background-size: cover;
}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>
<body>
  <h1 class="title">Gallery of Real Cool JS3 Images</h1>
  <div class="gallery-container">
    <ul class="gallery js-gallery">
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=1062')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=837')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=1025')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=237')"></li>
    </ul>
  </div>
  <script src="script.js" defer></script>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript on enter key searchbox 
Javascript :: bookmarklets 
Javascript :: javascript integer 
Javascript :: SHOPIFY LANGUAGE SELECTOR 
Javascript :: Find Smallest Number by function in Javascript 
Javascript :: array values js 
Javascript :: Mongoose filter by multiple fields 
Javascript :: setimeout 
Javascript :: tailwincss in react native 
Javascript :: create javascript array with 1 value 
Javascript :: javascript array randomizer 
Javascript :: 8.1.2. Array Length¶ 
Javascript :: download pdf javascript 
Javascript :: npm set author name 
Javascript :: javascript map to object 
Javascript :: template literals in javascript 
Javascript :: add multiple elements to set javascript 
Javascript :: group by in javascript 
Javascript :: foreach method of array in javascript 
Javascript :: jquery if else 
Javascript :: react background gradient 
Javascript :: how to stop canvas resizing from resizing images 
Javascript :: Vue 3 script setup props emits 
Javascript :: react native docs 
Javascript :: make canvas cover whole screen in html 
Javascript :: browser tab switch event js 
Javascript :: array some 
Javascript :: js math random 
Javascript :: handling event in jsx 
Javascript :: react native elements 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =