我正在嘗試制作照片幻燈片。我正在嘗試將計時器設定為每 5 秒運行一次setInterval(),我正在練習課程,不知道如何將其納入課程中。
const list = document.querySelector('.js-gallery');
const items = document.querySelectorAll('.gallery__item');
Array.from(list);
class Gallery {
constructor(slideshow) {
this.slideshow = slideshow;
this.slideCount = list.length;
this.items = items[0].getBoundingClientRect().width;
this.currentSlide = 1;
}
transitionSlide() {
if (this.currentSlide < this.slideCount) {
list.style.transform = `translateX(-${this.currentSlide * this.items}px)`;
this.currentSlide = 1;
} else {
list.style.transform = `translateX(0)`;
this.currentSlide = 1;
}
}
// Trying to make a method with setInterval() so that the slide runs every 5 seconds.
}
const pics = new Gallery(list);
.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"></script>
</body>
</html>
uj5u.com熱心網友回復:
這是下面的作業片段。我已經做到了,它每 0.5 秒轉換一次,這樣你就可以更快地看到它。如果您希望它在 5 秒,請將時間間隔更改為 5000 而不是 500。
通常,setInterval 在類中的作業方式與在類外的作業方式沒有任何不同。您只需要注意this系結(即,如果您這樣做了setInterval(this.transitionSlide, 5000),這將不起作用,因為this會失去范圍,因此.bind(this))
需要注意的關鍵事項:
您想跟蹤 setInterval 的回傳值,它是一個間隔 ID。這將使您停止間隔不斷發生。
我們現在有 2 個函式 - startSlideshow 和 stopSlideshow ,當你實體化一個 new 時
Gallery,你應該.startSlideshow()在你想要它啟動的時候使用實體變數。list.length當您需要使用 slideCount 時,您正在使用它,items.length因為這就是跟蹤單個幻燈片的原因。
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>
uj5u.com熱心網友回復:
嘗試這樣的事情:
class MyClass {
constructor() {
this.startInterval(); // start interval in your constructor
}
startInterval() {
const myInterval = setInterval(this.transitionSlide, 5000);
}
transitionSlide() {
// ... logic for slideshow
console.log("Slideshow transitioned.");
}
}
const cls = new MyClass();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/472073.html
標籤:javascript html 班级 哎呀
上一篇:PHP-隧道化API
