[英]How do I make an automatic image slideshow that I can pause and press next and backwards?
我嘗試將 W3 School 的代碼用於他們的示例 1 和自動播放幻燈片。 https://www.w3schools.com/howto/howto_js_slideshow.asp
嘗試后,我的幻燈片瘋狂地跳來跳去,下一個按鈕不再起作用。 我還想包含一個 function 以便能夠隨時暫停圖像。
到目前為止,這是我在 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>
我該如何做到這一點: 1.我有一個播放/暫停 function 2.圖像在自動播放時播放 5 秒 3.單擊左右可以 go 到上一個或下一個圖像
預先感謝您的幫助!
您可以使用引導程序更輕松地實現此輪播,請查看此鏈接以獲取更多信息: https://getbootstrap.com/docs/5.0/components/carousel/
這將起作用,
<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.