我正在尝试循环一些引号,以便它们继续反复显示,但是我遇到了麻烦,我在google和此处查看了其他一些帖子,但无法弄清楚如何更改代码下面提供了与我的代码一起使用的帖子。我使用了一个名为edit fiddle-JSFiddle的网站来测试代码,目前它仅在最后一个报价处停止。任何帮助,将不胜感激,谢谢。
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
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";
setTimeout(showSlides, 8000); // Change image every 10 seconds
}
* {
box-sizing: border-box
}
body {
font-family: Verdana, sans-serif;
margin: 0
}
/* Slideshow container */
.slideshow-container {
position: relative;
}
/* Slides */
.mySlides {
display: none;
padding: 80px;
text-align: center;
}
/* Add an italic font style to all quotes */
q {
font-style: italic;
}
/* Add a blue color to the author */
.author {
color: cornflowerblue;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="slideshow-container">
<div class="mySlides">
<q>I HATE EVERY MINUTE OF TRAINING. BUT I SAID, DON’T QUIT. SUFFER NOW
AND LIVE THE REST OF YOUR LIFE AS A CHAMPION.</q>
<p class="author">- MOHAMMAD ALI</p>
</div>
<div class="mySlides">
<q>YOU’RE THE ONLY ONE WHO CAN MAKE THE DIFFERENCE. WHATEVER YOUR DREAM
IS, GO FOR IT. </q>
<p class="author">– MAGIC JOHNSON.</p>
</div>
<div class="mySlides">
<q>IF YOU TURN UP WORRYING ABOUT HOW YOU’RE GOING TO PERFORM, YOU’VE
ALREADY LOST. TRAIN HARD, TURN UP, RUN YOUR BEST AND THE REST WILL TAKE
CARE OF ITSELF. </q>
<p class="author">– USAIN BOLT.</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>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</body>
</html>
答案 0 :(得分:0)
它没有循环,因为它由于脚本错误而崩溃:
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
这将超出数组中的元素数量。
您可以通过在递增之前对其进行检查来克服这一点:
if (slideIndex+1 > slides.length) {
slideIndex = 1;
}else
{
slideIndex++;
}
另外,您缺少跨度元素class =“ dot”,以匹配引号。
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
if (slideIndex+1 > slides.length) {
slideIndex = 1;
}else
{
slideIndex++;
}
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";
setTimeout(showSlides, 2000); // Change image every 10 seconds
}
* {
box-sizing: border-box
}
body {
font-family: Verdana, sans-serif;
margin: 0
}
/* Slideshow container */
.slideshow-container {
position: relative;
}
/* Slides */
.mySlides {
display: none;
padding: 80px;
text-align: center;
}
/* 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 HATE EVERY MINUTE OF TRAINING. BUT I SAID, DON’T QUIT. SUFFER NOW
AND LIVE THE REST OF YOUR LIFE AS A CHAMPION.</q>
<p class="author">- MOHAMMAD ALI</p>
</div>
<div class="mySlides">
<q>YOU’RE THE ONLY ONE WHO CAN MAKE THE DIFFERENCE. WHATEVER YOUR DREAM
IS, GO FOR IT. </q>
<p class="author">– MAGIC JOHNSON.</p>
</div>
<div class="mySlides">
<q>IF YOU TURN UP WORRYING ABOUT HOW YOU’RE GOING TO PERFORM, YOU’VE
ALREADY LOST. TRAIN HARD, TURN UP, RUN YOUR BEST AND THE REST WILL TAKE
CARE OF ITSELF. </q>
<p class="author">– USAIN BOLT.</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>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
答案 1 :(得分:0)
您可以在showSlides()
函数if (slideIndex > slides.length) {slideIndex = 1}
中将此条件更改为:
if (slideIndex > slides.length - 1) {
slideIndex = 1;
}
将幻灯片索引重置为1,以便幻灯片可以不断更改。
答案 2 :(得分:0)
您有4张幻灯片,但只有3个“点”。当显示第4张幻灯片时,它将尝试显示不存在的第4个点。
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
// Hide all slides
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
// Hide all dots
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
// Show current slide
slides[slideIndex].style.display = "block";
dots[slideIndex].className += " active";
// Increase slide index
slideIndex++;
if (slideIndex == slides.length) {
slideIndex = 0;
}
// Wait, then show next slide
setTimeout(showSlides, 1000);
}
* {
box-sizing: border-box
}
body {
font-family: Verdana, sans-serif;
margin: 0
}
/* Slideshow container */
.slideshow-container {
position: relative;
}
/* Slides */
.mySlides {
display: none;
padding: 80px;
text-align: center;
}
/* 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 HATE EVERY MINUTE OF TRAINING. BUT I SAID, DON’T QUIT. SUFFER NOW AND LIVE THE REST OF YOUR LIFE AS A CHAMPION.</q>
<p class="author">- MOHAMMAD ALI</p>
</div>
<div class="mySlides">
<q>YOU’RE THE ONLY ONE WHO CAN MAKE THE DIFFERENCE. WHATEVER YOUR DREAM IS, GO FOR IT. </q>
<p class="author">– MAGIC JOHNSON.</p>
</div>
<div class="mySlides">
<q>IF YOU TURN UP WORRYING ABOUT HOW YOU’RE GOING TO PERFORM, YOU’VE ALREADY LOST. TRAIN HARD, TURN UP, RUN YOUR BEST AND THE REST WILL TAKE CARE OF ITSELF. </q>
<p class="author">– USAIN BOLT.</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>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>