HTML/Javascript文本幻灯片,其中包含无限数量的组件



我试图为我的网站制作一个文本幻灯片。我使用了以下代码,可以制作一个幻灯片,里面有3组文本。

问题是:要显示3组文本,我需要在html文件中使用三组<div class="mySlides">。如果我有一个包含100个单词的文本列表(如[猫、狗、椅子、树等](,我如何在不写100组<div class="mySlides">的情况下更改代码来管理它。

感谢

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");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";

}
/* Slideshow container */
.slideshow-container {
position: relative;
background: #f1f1f1f1;
}

/* Slides */
.mySlides {
display: none;
padding: 200px;
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;
}

/* 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 words */
.myText {color: cornflowerblue;}
<div class="slideshow-container">

<!-- Full-width slides/quotes -->
<div class="mySlides">
<p class="myText">Cat</p>
</div>

<div class="mySlides">
<p class="myText">Dog</p>
</div>

<div class="mySlides">
<p class="myText">Chair</p>
</div>

<!-- Next/prev buttons -->
<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

这个答案创建一个单词数组。

然后循环遍历它,生成DIV和p,并将其附加到幻灯片放映容器中。

words = ["cat","dog","mouse","snake","tree"];
container = document.querySelector(".slideshow-container");
words.forEach(function(word){
div = document.createElement("div");
div.classList.add("mySlides");
p = document.createElement("p");
p.classList.add("myText");
p.innerHTML = word;
div.appendChild(p)
container.appendChild(div)
});
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");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";

}
/* Slideshow container */
.slideshow-container {
position: relative;
background: #f1f1f1f1;
}

/* Slides */
.mySlides {
display: none;
padding: 200px;
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;
}

/* 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 words */
.myText {color: cornflowerblue;}
<div class="slideshow-container">

<!-- Next/prev buttons -->
<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

最新更新