带箭头和按钮的自动幻灯片显示

时间:2018-11-06 19:32:29

标签: javascript html css

我从w3schools的幻灯片制作“ HOW TO”指南开始,尽管它提供了自动幻灯片显示或带有控制箭头和幻灯片指示器的选项,但并没有告诉您如何同时进行。

在这里进行了一些搜索之后,我发现了这个(这篇文章底部的代码)-Automatic slideshow with button-但是解决方案有两个问题:

  1. 如果我单击底部的一个指示器按钮,该按钮不会自动显示出来,则幻灯片将成功跳转到该项目,但不会继续到我所显示的幻灯片之后刚刚点击;相反,如果我从未单击任何按钮,它将转到下一张幻灯片。如果使用箭头,则存在相同的问题。底部的按钮更容易用作示例。
    例如我有标有A,B,C和D的幻灯片。页面在幻灯片A上加载并启动我。经过指定的时间间隔,幻灯片将自身移动到B。单击幻灯片指示器按钮以移动到幻灯片A ,然后将我带到A。然后页面将我带到C,就好像我从未单击过A。我希望幻灯片从我单击的位置(A)开始而不是从幻灯片继续在我单击它之前,它有“打算”去的地方(C)。
  2. 如果仅在计数器中的某些时间过去后单击任何按钮来更改幻灯片,则计数器不会重置;就像我没有换过幻灯片一样继续播放,然后在它再次自动移动时重新开始。
    例如自动更改幻灯片之前的时间为5秒。我仅在观看幻灯片A的3秒钟后单击向右箭头。然后,幻灯片显示为幻灯片B ,但只剩下了2秒钟,然后才进行幻灯片C。

我尝试了许多解决方案,例如创建用于重置间隔计时器的函数,但是所有这些最终都破坏了代码,并使幻灯片显示变得混乱。我会在Codepen上发布一个链接,以给出一个示例,但是自从我今天早些时候写完笔以来,一旦我进入个人资料的“笔”页面,该站点就会停止工作。 (如果您真的有兴趣,请转到Codepen并查找我-用户名是lgcorpora-并尝试单击名为“带有按钮的自动幻灯片”的笔-应该在顶部附近。)

var slideIndex = 0;
showSlides();
var slides,dots;

function showSlides() {
    var i;
    slides = document.getElementsByClassName("mySlides");
    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, 4000); // Change image every 8 seconds
}

function plusSlides(position) {
    slideIndex +=position;
    if (slideIndex> slides.length) {slideIndex = 1}
    else if(slideIndex<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";
}

function currentSlide(index) {
    if (index> slides.length) {index = 1}
    else if(index<1){index = 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[index-1].style.display = "block";  
    dots[index-1].className += " active";
}
#slide{
	width:96%;
	}
	
* {
	box-sizing: border-box
}

.mySlides {
	display: none
}

.slideshow-container {
	width: auto;
	position: relative;
	margin: -10px;

}
/* Next & previous buttons */
.prev, .next {
	cursor: pointer;
	position: absolute;
	top: 50%;
	width: auto;
	padding: 16px;
	margin-top: -22px;
	color: #494B55;
	font-weight: bold;
	font-size: 18px;
	transition: 0.6s ease;
	border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
	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);
}
/* Caption text */
.text {
	color: #f2f2f2;
	font-size: 15px;
	padding: 8px 12px;
	position: absolute;
	bottom: 8px;
	width: 100%;
	text-align: center;
}

/* The dots/bullets/indicators */
.dot {
	cursor: pointer;
	height: 13px;
	width: 13px;
	margin: 0 2px;
	background-color: #bbb;
	border-radius: 50%;
	display: inline-block;
	transition: background-color 0.6s ease;
}
.active, .dot:hover {
	background-color: #494B55;
}
/* Fading animation */
.fade {
	-webkit-animation-name: fade;
	-webkit-animation-duration: 2s;
	animation-name: fade;
	animation-duration: 2s;
}
@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {font-size: 11px}
}

.pics
{
    border:3px solid #494B55;
    width:200px;
}
<div class="ss-container">
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_5terre.jpg" class="pics"/>
                </div>
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_forest.jpg" class="pics"/>
                </div>
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_lights.jpg" class="pics"/>
                </div>
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_mountains.jpg" class="pics"/>
                </div>
                <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
                <a class="next" onclick="plusSlides(1)">&#10095;</a>
            </div>
            
            <!--    Putting in the click dots   -->
            <div style="text-align:center">
                <span class="dot" onclick="currentSlide(1)"></span>
                <span class="dot" onclick="currentSlide(2)"></span>
                <span class="dot" onclick="currentSlide(3)"></span>
                <span class="dot" onclick="currentSlide(4)"></span>
            </div>

1 个答案:

答案 0 :(得分:0)

您必须在slideIndex函数中设置currentSlide。另外,将超时值放入变量(timer)中,以便在手动显示幻灯片时可以将其停止。

var slideIndex = 0;
showSlides();
//add the global timer variable
var slides,dots,timer;

function showSlides() {
    var i;
    slides = document.getElementsByClassName("mySlides");
    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";
    //put the timeout in the timer variable
    timer = setTimeout(showSlides, 4000); // Change image every 8 seconds
}

function plusSlides(position) {
    //clear/stop the timer
    clearTimeout(timer);
    slideIndex +=position;
    if (slideIndex> slides.length) {slideIndex = 1}
    else if(slideIndex<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";
    //create a new timer
    timer = setTimeout(showSlides, 4000);
}

function currentSlide(index) {
    //clear/stop the timer
    clearTimeout(timer);
    if (index> slides.length) {index = 1}
    else if(index<1){index = slides.length}
    //set the slideIndex with the index of the function
    slideIndex = index;
    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[index-1].style.display = "block";  
    dots[index-1].className += " active";
    //create a new timer
    timer = setTimeout(showSlides, 4000);
}
#slide{
	width:96%;
	}
	
* {
	box-sizing: border-box
}

.mySlides {
	display: none
}

.slideshow-container {
	width: auto;
	position: relative;
	margin: -10px;

}
/* Next & previous buttons */
.prev, .next {
	cursor: pointer;
	position: absolute;
	top: 50%;
	width: auto;
	padding: 16px;
	margin-top: -22px;
	color: #494B55;
	font-weight: bold;
	font-size: 18px;
	transition: 0.6s ease;
	border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
	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);
}
/* Caption text */
.text {
	color: #f2f2f2;
	font-size: 15px;
	padding: 8px 12px;
	position: absolute;
	bottom: 8px;
	width: 100%;
	text-align: center;
}

/* The dots/bullets/indicators */
.dot {
	cursor: pointer;
	height: 13px;
	width: 13px;
	margin: 0 2px;
	background-color: #bbb;
	border-radius: 50%;
	display: inline-block;
	transition: background-color 0.6s ease;
}
.active, .dot:hover {
	background-color: #494B55;
}
/* Fading animation */
.fade {
	-webkit-animation-name: fade;
	-webkit-animation-duration: 2s;
	animation-name: fade;
	animation-duration: 2s;
}
@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {font-size: 11px}
}

.pics
{
    border:3px solid #494B55;
    width:200px;
}
<div class="ss-container">
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_5terre.jpg" class="pics"/>
                </div>
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_forest.jpg" class="pics"/>
                </div>
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_lights.jpg" class="pics"/>
                </div>
                <div class="mySlides fade">
                    <img src="https://www.w3schools.com/css/img_mountains.jpg" class="pics"/>
                </div>
                <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
                <a class="next" onclick="plusSlides(1)">&#10095;</a>
            </div>
            
            <!--    Putting in the click dots   -->
            <div style="text-align:center">
                <span class="dot" onclick="currentSlide(1)"></span>
                <span class="dot" onclick="currentSlide(2)"></span>
                <span class="dot" onclick="currentSlide(3)"></span>
                <span class="dot" onclick="currentSlide(4)"></span>
            </div>