[英]How to make this slider show 2 quotes upon clicking on next and prev
Currently it doesn't work as intended. 目前,它无法正常工作。 I am trying to make the slide show show 2 slide in one line at any given time and when the user clicks on next and prev arrows then next 2 or previous two slides should show depending which one was clicked.
我试图在任何给定时间使幻灯片显示在同一行显示2张幻灯片,并且当用户单击下一张和上一张箭头时,下一张2张或上一张两张幻灯片应显示在哪张幻灯片上。 There can be total any number of slider but max two should show at a time.
总共可以有任意数量的滑块,但一次最多可以显示两个。
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * {box-sizing: border-box} body {font-family: Verdana, sans-serif; margin:0} /* Slideshow container */ .slideshow-container { position: relative; background: #f1f1f1f1; } /* Slides */ .mySlides { display: none; padding: 80px; text-align: center; width: 50%; } /* Next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -30px; padding: 16px; color: #888; font-weight: bold; font-size: 20px; border-radius: 0 3px 3px 0; user-select: none; } /* Position the "next button" to the right */ .next { position: absolute; 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); color: white; } /* Add a blue color to the author */ .author {color: cornflowerblue;} </style> </head> <body> <div class="slideshow-container"> <div class="mySlides"> <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q> <p class="author">- John Keats</p> </div> <div class="mySlides"> <q>But man is not made for defeat. A man can be destroyed but not defeated.</q> <p class="author">- Ernest Hemingway</p> </div> <div class="mySlides"> <q>I have not failed. I've just found 10,000 ways that won't work.</q> <p class="author">- Thomas A. Edison</p> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <script> var slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); if (n > slides.length) {slideIndex = 1} if (n < 1) {slideIndex = slides.length} for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } slides[slideIndex-1].style.display = "block"; slides[slideIndex-2].style.display = "block"; } </script> </body> </html>
I've changed a few things in your code, and should works fine, but I recommend you using any library with slideshows. 我已经更改了代码中的一些内容,并且应该可以正常工作,但是我建议您使用任何具有幻灯片显示功能的库。
function showSlides(n) {
if(n>=slides.length){
n=0;
slideIndex = 0
} else if(n<0 && slides.length%2===0){
n=slides.length-2;
slideIndex = slides.length-2
} else if(n<0 && slides.length%2===1) {
n=slides.length-1;
slideIndex = slides.length-1
}
let slidesShow = Array.from(slides).slice(n,n+2);
Array.from(slides).forEach(item=>{
item.style.display='none'
})
for(let i= 0; i< slidesShow.length;i++){
slidesShow[i].style.display = 'block'
}
}
next.addEventListener('click', function(e){
slideIndex = slideIndex+2
showSlides(slideIndex);
})
prev.addEventListener('click', function(e){
slideIndex = slideIndex-2
showSlides(slideIndex);
})
Here is a workin fiddle: https://jsfiddle.net/1kL9t0g8/ 这是一个工作提琴: https : //jsfiddle.net/1kL9t0g8/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.