#javascript #html #css
#javascript #HTML #css
Вопрос:
Я попытался сделать текстовое слайд-шоу для своего сайта. Я использовал следующие коды и могу создать слайд-шоу с 3 наборами текстов внутри него.
Проблема в том, что для отображения 3 наборов текста мне нужно три набора <div class="mySlides">
в моем HTML-файле. Если у меня есть список текстов со 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 amp; 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)">amp;#10094;</a>
<a class="next" onclick="plusSlides(1)">amp;#10095;</a>
</div>
Комментарии:
1. Итак, перебирайте свой массив и создавайте элементы?
Ответ №1:
Этот ответ создает массив слов.
Затем перебирает его и генерирует 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 amp; 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)">amp;#10094;</a>
<a class="next" onclick="plusSlides(1)">amp;#10095;</a>
</div>
Комментарии:
1. Большое вам спасибо. Это именно то, что мне нужно!