gpt4 book ai didi

javascript - 如何添加另一个幻灯片?

转载 作者:可可西里 更新时间:2023-11-01 13:41:12 25 4
gpt4 key购买 nike

我是 web 编码的新手,我不知道如何向 w3school 示例添加另一个幻灯片……见下文。如果有任何帮助,我将不胜感激。

我知道,我必须将另一个幻灯片类添加到我的 html 中,但我不知道我需要在 javaScript 中更改什么,我尝试添加另一个索引但没有成功...

亲切的问候!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
.mySlides1, .mySlides2 {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 grey background color */
.prev:hover, .next:hover {
background-color: #f1f1f1;
color: black;
}
</style>
</head>
<body>

<h2 style="text-align:center">Multiple Slideshows</h2>

<p>Slideshow 1:</p>
<div class="slideshow-container">
<div class="mySlides1">
<img src="img_nature_wide.jpg" style="width:100%">
</div>

<div class="mySlides1">
<img src="img_snow_wide.jpg" style="width:100%">
</div>

<div class="mySlides1">
<img src="img_mountains_wide.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1, 0)">&#10094;</a>
<a class="next" onclick="plusSlides(1, 0)">&#10095;</a>
</div>

<p>Slideshow 2:</p>
<div class="slideshow-container">
<div class="mySlides2">
<img src="img_band_chicago.jpg" style="width:100%">
</div>

<div class="mySlides2">
<img src="img_band_la.jpg" style="width:100%">
</div>

<div class="mySlides2">
<img src="img_band_ny.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a>
<a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
</div>

<script>
var slideIndex = [1,1];
var slideId = ["mySlides1", "mySlides2"]
showSlides(1, 0);
showSlides(1, 1);

function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
}

function showSlides(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {slideIndex[no] = 1}
if (n < 1) {slideIndex[no] = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no]-1].style.display = "block";
}
</script>

</body>
</html>

我像这样更改了我的代码以实现第三个幻灯片放映。不幸的是,我的幻灯片只显示一张图片,点击下一张时什么也没有发生...有人可以帮我找到解决方案吗?

<p>Slideshow 3:</p>
<div class="slideshow-container">
<div class="mySlides3">
<img src="img_band_chicago.jpg" style="width:100%">
</div>

<div class="mySlides3">
<img src="img_band_la.jpg" style="width:100%">
</div>

<div class="mySlides3">
<img src="img_band_ny.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-3, 2)">&#10094;</a>
<a class="next" onclick="plusSlides(3, 2)">&#10095;</a>
</div>

<script>
var slideIndex = [3,3];
var slideId = ["mySlides1", "mySlides2", "mySlides3"]
showSlides(1, 0);
showSlides(1, 1);
showSlides(3, 2);

function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
}

function showSlides(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {slideIndex[no] = 1}
if (n < 1) {slideIndex[no] = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no]-1].style.display = "block";
}
</script>

最佳答案

添加 html:

<p>Slideshow 3:</p>
<div class="slideshow-container">
<div class="mySlides3">
<img src="img_band_chicago.jpg" style="width:100%">
</div>

<div class="mySlides3">
<img src="img_band_la.jpg" style="width:100%">
</div>

<div class="mySlides3">
<img src="img_band_ny.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1, 2)">&#10094;</a>
<a class="next" onclick="plusSlides(1, 2)">&#10095;</a>
</div>

将 slideid 更改为:

var slideId = ["mySlides1", "mySlides2", "mySlides3"]

添加这个:

showSlides(1, 2);

如果更改为:

if (n < 2) {slideIndex[no] = x.length}

最终结果应该是这样的:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
.mySlides1, .mySlides2 {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 grey background color */
.prev:hover, .next:hover {
background-color: #f1f1f1;
color: black;
}
</style>
</head>
<body>

<h2 style="text-align:center">Multiple Slideshows</h2>

<p>Slideshow 1:</p>
<div class="slideshow-container">
<div class="mySlides1">
<img src="img_nature_wide.jpg" style="width:100%">
</div>

<div class="mySlides1">
<img src="img_snow_wide.jpg" style="width:100%">
</div>

<div class="mySlides1">
<img src="img_mountains_wide.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1, 0)">&#10094;</a>
<a class="next" onclick="plusSlides(1, 0)">&#10095;</a>
</div>

<p>Slideshow 2:</p>
<div class="slideshow-container">
<div class="mySlides2">
<img src="img_band_chicago.jpg" style="width:100%">
</div>

<div class="mySlides2">
<img src="img_band_la.jpg" style="width:100%">
</div>

<div class="mySlides2">
<img src="img_band_ny.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a>
<a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
</div>

<p>Slideshow 3:</p>
<div class="slideshow-container">
<div class="mySlides3">
<img src="img_band_chicago.jpg" style="width:100%">
</div>

<div class="mySlides3">
<img src="img_band_la.jpg" style="width:100%">
</div>

<div class="mySlides3">
<img src="img_band_ny.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1, 2)">&#10094;</a>
<a class="next" onclick="plusSlides(1, 2)">&#10095;</a>
</div>

<script>
var slideIndex = [1,1];
var slideId = ["mySlides1", "mySlides2", "mySlides3"]
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2);

function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
}

function showSlides(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {slideIndex[no] = 1}
if (n < 2) {slideIndex[no] = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no]-1].style.display = "block";
}
</script>

</body>
</html>

关于javascript - 如何添加另一个幻灯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57964492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com