简体   繁体   English

添加多个图像幻灯片

[英]Adding Multiple Image Slideshows

I have this worksheet to complete and one of the questions is to explain this code and to add one more image slideshow.我有这个工作表要完成,其中一个问题是解释这个代码并添加一个更多的图像幻灯片。

I don't really understand the javascript code and a bit confused as to how to add another image slideshow.我不太了解 javascript 代码,对如何添加另一个图像幻灯片有点困惑。

I understand the HTML and css but the javascript is confusing.我了解 HTML 和 css 但 javascript 令人困惑。 Any help would be appreciated.任何帮助,将不胜感激。

 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"; }
 * {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; }
 <h2 style="text-align:center">Multiple Slideshows</h2> <p>Slideshow 1:</p> <div class="slideshow-container"> <div class="mySlides1"> <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" style="width:100%"> </div> <div class="mySlides1"> <img src="https://homepages.cae.wisc.edu/~ece533/images/arctichare.png" style="width:100%"> </div> <div class="mySlides1"> <img src="https://homepages.cae.wisc.edu/~ece533/images/boat.png" 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="https://homepages.cae.wisc.edu/~ece533/images/pool.png" style="width:100%"> </div> <div class="mySlides2"> <img src="https://homepages.cae.wisc.edu/~ece533/images/sails.png" style="width:100%"> </div> <div class="mySlides2"> <img src="https://homepages.cae.wisc.edu/~ece533/images/watch.png" style="width:100%"> </div> <a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a> <a class="next" onclick="plusSlides(1, 1)">&#10095;</a> </div>

I updated the code using an IDE to be human readable.我使用 IDE 更新了代码,使其易于阅读。 I believe it is pretty straight forward now.我相信现在很简单。 If you don't understand specific parts you can ask me in comments.如果你不明白具体部分,你可以在评论中问我。 (This not quality code, I believe it's a learning exercise so I kept it as it is) (这不是质量代码,我相信这是一个学习练习,所以我保持原样)

 var slideIndex = [1, 1, 1]; var slideContainerSelectors = ["mySlides1", "mySlides2", "mySlides3"] function showSlides(nextslideIndex, slideContainerIndex) { var i; var slideContainerElement = document.getElementsByClassName(slideContainerSelectors[slideContainerIndex]); if (nextslideIndex > slideContainerElement.length) { slideIndex[slideContainerIndex] = 1 } if (nextslideIndex < 1) { slideIndex[slideContainerIndex] = slideContainerElement.length } for (i = 0; i < slideContainerElement.length; i++) { slideContainerElement[i].style.display = "none"; } slideContainerElement[slideIndex[slideContainerIndex] - 1].style.display = "block"; } function plusSlides(increment, index) { showSlides(slideIndex[index] += increment, index); //----------------------------^ slideIndex[index] = slideIndex[index] + increment } showSlides(1, 0); showSlides(1, 1); showSlides(1, 2);
 * { 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; }
 <h2 style="text-align:center">Multiple Slideshows</h2> <p>Slideshow 1:</p> <div class="slideshow-container"> <div class="mySlides1"> <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" style="width:100%"> </div> <div class="mySlides1"> <img src="https://homepages.cae.wisc.edu/~ece533/images/arctichare.png" style="width:100%"> </div> <div class="mySlides1"> <img src="https://homepages.cae.wisc.edu/~ece533/images/boat.png" 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="https://homepages.cae.wisc.edu/~ece533/images/pool.png" style="width:100%"> </div> <div class="mySlides2"> <img src="https://homepages.cae.wisc.edu/~ece533/images/sails.png" style="width:100%"> </div> <div class="mySlides2"> <img src="https://homepages.cae.wisc.edu/~ece533/images/watch.png" 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="https://picsum.photos/200/300" style="width:100%"> </div> <div class="mySlides3"> <img src="https://picsum.photos/id/237/200/300" style="width:100%"> </div> <div class="mySlides3"> <img src="https://picsum.photos/id/238/200/300" style="width:100%"> </div> <a class="prev" onclick="plusSlides(-1, 2)">&#10094;</a> <a class="next" onclick="plusSlides(1, 2)">&#10095;</a> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM