简体   繁体   English

单击下一个和上一个后,如何使此滑块显示2个引号

[英]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.

相关问题 如何在弹出窗口上制作带有下一个和上一个按钮导航的模态滑块 - How to make a modal slider with next and prev buttons navigation on popup jQuery-在滑块中显示/隐藏上一个/下一个按钮 - jQuery - Show/Hide prev/next buttons in slider 如何在单击下一个和上一个按钮时增加和减少范围 slider 输入的值(香草 JavaScript) - how to increase and decrease the value of range slider input on clicking next and prev button (vanilla JavaScript) 单击上一个/下一个箭头时,JS滑块图像消失 - JS slider image disappears when clicking Prev/Next arrow 如何在单击上一个和下一个按钮时调用事件? - How to call events on clicking prev and next button? 滑块导航-下一个上一个 - Slider navigation - next prev 如何通过javascript和jQuery使文本滚动(如通过单击上一个下一个图标来滑动文本) - how to make text scroll ( like text to slide by clicking prev n next icon) through javascript and jQuery 使用next,prev按钮制作简单的jQuery图像滑块 - make simple jQuery image Slider with next, prev button 如何在幻灯片放映中添加上一个和下一个按钮 - How to add prev and next button in slide show jQuery prev / next滑块 - &gt;如何在第一张/最后一张幻灯片上禁用prev / next? - jQuery prev/next slider --> how to disable prev/next on first/last slide?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM