[英]How do I make an automatic image slideshow that I can pause and press next and backwards?
I tried combining the codes from W3 School for their example 1 and auto play slideshow.我尝试将 W3 School 的代码用于他们的示例 1 和自动播放幻灯片。 https://www.w3schools.com/howto/howto_js_slideshow.asp
https://www.w3schools.com/howto/howto_js_slideshow.asp
Upon trying, my slide show jumps around madly and the next button does not work anymore.尝试后,我的幻灯片疯狂地跳来跳去,下一个按钮不再起作用。 I also want to include a function to be able to pause the image whenever I want.
我还想包含一个 function 以便能够随时暂停图像。
This is my code from W3 school so far:到目前为止,这是我在 W3 学校的代码:
var slideIndex = 0; 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"); 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, 2000); }
* {box-sizing: border-box} body {font-family: Verdana, sans-serif; margin:0}.mySlides {display: none} img {vertical-align: middle;} /* Slideshow container */.slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* Next & previous buttons */.prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } /* 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; } /* Number text (1/3 etc) */.numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* 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; }.active, .dot:hover { background-color: #717171; } /* Fading animation */.fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-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) {.prev, .next,.text {font-size: 11px} }
<div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="image1.jpg" style="width:100%"> <div class="text">Codes</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="image2.jpg" style="width:100%"> <div class="text">What I have learnt</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="image3.jpg" style="width:100%"> <div class="text">CSS Style</div> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <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> </div>
How do I make it so: 1.I have a play/pause function 2.Images play for 5 seconds on auto play 3.Clicking left and right is able to go to previous or next image我该如何做到这一点: 1.我有一个播放/暂停 function 2.图像在自动播放时播放 5 秒 3.单击左右可以 go 到上一个或下一个图像
Thank you in advance for your help!预先感谢您的帮助!
You can use bootstrap to implement this carousel more more easier check this link out for more info: https://getbootstrap.com/docs/5.0/components/carousel/您可以使用引导程序更轻松地实现此轮播,请查看此链接以获取更多信息: https://getbootstrap.com/docs/5.0/components/carousel/
This will work,这将起作用,
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
/* 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;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* 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;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-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) {
.prev, .next,.text {font-size: 11px}
}
</style>
</head>
<body>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="https://cdn.pixabay.com/photo/2014/09/14/18/04/dandelion-445228__340.jpg" style="width:100%">
<div class="text">Codes</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="https://cdn.cjr.org/wp-content/uploads/2019/07/AdobeStock_100000042-e1563305717660-686x371.jpeg" style="width:100%">
<div class="text">What I have learnt</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="https://static-cse.canva.com/blob/142356/removing-background-images_Unsplash.bad043c2.jpeg" style="width:100%">
<div class="text">CSS Style</div>
</div>
<a class="prev" onclick="toggleSlides(-1)">❮</a>
<a class="next" onclick="toggleSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="showSlides(1,true)"></span>
<span class="dot" onclick="showSlides(2,true))"></span>
<span class="dot" onclick="showSlides(3,true))"></span>
</div>
<script>
var doAnimation = true;
var slideIndex = 0;
showSlides(slideIndex);
function toggleSlides(n) {
showSlides(slideIndex + n, true);
}
Array.from(document.getElementsByClassName("mySlides")).forEach((mySlide)=> {
mySlide.addEventListener("mouseover",function() {
doAnimation = false;
})
mySlide.addEventListener("mouseout",function() {
doAnimation = true;
});
});
var timer;
function showSlides(n, force) {
console.log(doAnimation)
if (doAnimation || force) {
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 (n) {
slideIndex = n;
}
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";
if (force) {
clearInterval(timer);
timer = setInterval(showSlides, 5000);
}
}
}
timer = setInterval(showSlides, 5000);
</script>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.