如何防止将相同的文本传递到同一页面上的另一个幻灯片?



我想在我的HTML/PHP应用程序中集成这个引号幻灯片:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_quotes_slideshow.

它工作得很好,但问题是我希望这个幻灯片显示10次,每次包含不同的文本。

对于第一个帖子,我有多个包含用户帖子的卡片,一切正常,但当我按下"下一个"按钮连续4次,它会转到下一个帖子(所以到下一张卡片),再次显示相同的内容。

如何个性化每个幻灯片,如何防止将相同的文本传递到其他卡片/帖子?

var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(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";
}
* {
box-sizing: border-box
}
body {
font-family: Verdana, sans-serif;
margin: 0
}

/* Slideshow container */
.slideshow-container {
position: fixed;
margin: auto;
width: 400px;
height: 200px;
background: #f1f1f1f1;
}

/* Slides */
.mySlides {
display: none;
padding: 80px;
text-align: center;
}

/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -30px;
padding: 16px;
color: #888;
font-weight: bold;
font-size: 20px;
border-radius: 0 3px 3px 0;
user-select: none;
}

/* Position the "next button" to the right */
.next {
position: absolute;
right: 0;
border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
color: white;
}

/* The dot/bullet/indicator container */
.dot-container {
text-align: center;
padding: 20px;
background: #ddd;
position: fixed;
margin-top: 200px;
width: 400px;
height: 50px;
}

/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}

/* Add a background color to the active dot/circle */
.active,
.dot:hover {
background-color: #717171;
}

/* Add an italic font style to all quotes */
q {
font-style: italic;
}

/* Add a blue color to the author */
.author {
color: cornflowerblue;
}
<div class="slideshow-container">
<div class="mySlides">
<q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
<p class="author">- John Keats</p>
</div>
<div class="mySlides">
<q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
<p class="author">- Ernest Hemingway</p>
</div>
<div class="mySlides">
<q>I have not failed. I've just found 10,000 ways that won't work.</q>
<p class="author">- Thomas A. Edison</p>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<div class="dot-container">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>

你可以这样做:

const slideshows = document.querySelectorAll(".slideshow");
slideshows.forEach((slideshow) => {
var dots = slideshow.querySelectorAll(".dot");
var prev = slideshow.querySelector(".prev");
var next = slideshow.querySelector(".next");
var slideIndex = 1;
showSlides(slideIndex);
prev.addEventListener("click", () => showSlides((slideIndex -= 1)));
next.addEventListener("click", () => showSlides((slideIndex += 1)));
dots.forEach((dot, i) => {
dot.addEventListener("click", () => showSlides((slideIndex = i + 1)));
});
function showSlides(n) {
var i;
var slides = slideshow.getElementsByClassName("mySlides");
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";
}
});
* {
box-sizing: border-box;
}
body {
font-family: Verdana, sans-serif;
margin: 0;
}
.slideshow {
display: inline-block;
margin: 10px;
}

/* Slideshow container */
.slideshow-container {
/*    position: fixed;
margin: auto; */
position: relative;
width: 400px;
height: 200px;
background: #f1f1f1f1;
}

/* Slides */
.mySlides {
display: none;
padding: 80px;
text-align: center;
padding-bottom: 0;
}

/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -30px;
padding: 16px;
color: #888;
font-weight: bold;
font-size: 20px;
border-radius: 0 3px 3px 0;
user-select: none;
}

/* Position the "next button" to the right */
.next {
position: absolute;
right: 0;
border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
color: white;
}

/* The dot/bullet/indicator container */
.dot-container {
text-align: center;
padding: 20px;
background: #ddd;
/*    position: fixed;
margin-top: 200px; */
width: 400px;
height: 50px;
}

/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}

/* Add a background color to the active dot/circle */
.active,
.dot:hover {
background-color: #717171;
}

/* Add an italic font style to all quotes */
q {
font-style: italic;
}

/* Add a blue color to the author */
.author {
color: cornflowerblue;
}
<div class="slideshow">
<div class="slideshow-container">
<div class="mySlides">
<q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
<p class="author">- John Keats</p>
</div>
<div class="mySlides">
<q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
<p class="author">- Ernest Hemingway</p>
</div>
<div class="mySlides">
<q>I have not failed. I've just found 10,000 ways that won't work.</q>
<p class="author">- Thomas A. Edison</p>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>
<div class="dot-container">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<div class="slideshow">
<div class="slideshow-container">
<div class="mySlides">
<q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
<p class="author">- John Keats</p>
</div>
<div class="mySlides">
<q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
<p class="author">- Ernest Hemingway</p>
</div>
<div class="mySlides">
<q>I have not failed. I've just found 10,000 ways that won't work.</q>
<p class="author">- Thomas A. Edison</p>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>
<div class="dot-container">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<div class="slideshow">
<div class="slideshow-container">
<div class="mySlides">
<q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
<p class="author">- John Keats</p>
</div>
<div class="mySlides">
<q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
<p class="author">- Ernest Hemingway</p>
</div>
<div class="mySlides">
<q>I have not failed. I've just found 10,000 ways that won't work.</q>
<p class="author">- Thomas A. Edison</p>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>
<div class="dot-container">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>

👉CodePen

最新更新