[英]Go to a specific slide on a slideshow with a button
Is there a way that I can make a button turn the slideshow that I have to a specific slide, as in I press the button and the slideshow will display slide 5 for example? 有没有一种方法可以使我将必须的幻灯片放到特定的幻灯片上,例如按下按钮,然后幻灯片将显示幻灯片5?
I've tried to just make a button with onlick=showDivs(5)
as suggested by another person, but that just seems to refresh the current slide, instead of showing the fifth one 我试图按照另一个人的建议用onlick=showDivs(5)
制作一个按钮,但这似乎只是刷新了当前幻灯片,而不是显示第五张幻灯片
Here's my slideshow code: 这是我的幻灯片代码:
<div>
<img class="mySlides" src="1.jpg">
<img class="mySlides" src="2.jpg">
<img class="mySlides" src="3.jpg">
</div>
<button class="w3-button w3-display-left" onclick="plusDivs(-1)">Previous</button>
<button class="w3-button w3-display-right" onclick="plusDivs(+1)">Next</button>
<script type="text/javascript">
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length};
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex-1].style.display = "block";
}
setInterval(function(){
$('.mySlides').click()
},1000)
</script>
You want to write the function like this 你想这样写函数
<script type="text/javascript">
var slideIndex = 1;
showDivs(index) {
slideIndex = index;
}
function plusDivs(n) {
showDivs(slideIndex += n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length};
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex-1].style.display = "block";
}
setInterval(function(){
$('.mySlides').click()
},1000)
</script>
我只是通过尝试随机的事情自己找到了答案,最后我将“ onclick =“ showDivs(slideIndex = 5)”添加到一个简单的按钮中,它起作用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.