简体   繁体   English

显示最后一张幻灯片时,如何使此javascript停止

[英]How can I make this javascript stop when the last slide is shown

I want to make this javascript stop when the last slide is shown, so that the next button won't work, but same with the previous button when the first slide is shown. 我想在显示最后一张幻灯片时使此javascript停止,以便下一个按钮不起作用,但与在显示第一张幻灯片时的上一个按钮相同。 For the previous button, when I press it and the slider is at the first slide I want it to stay to the current slide. 对于上一个按钮,当我按下它并且滑块在第一张幻灯片上时,我希望它停留在当前幻灯片上。

Javascript code: JavaScript代码:

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
    showDivs(slideIndex += n);
}

function showDivs(n) {
    var i;
    var x = document.getElementsByClassName('slide');
    if (n < 1) {slideIndex = 1;}
    for (i = 0; i < x.length; i++) {
        x[i].style.display = 'none';
    }
    x[slideIndex-1].style.display = 'block';
    }

HTML code: HTML代码:

<div class="links-to-wbp">
                    <div class="slide">
                        <a href="/physics/index.html"><img class="wbp" alt="thumb1" src="img/thumb1.png"/></a><a href="/francais/index.html"><img class="wbp" alt="thumb2" src="img/thumb2.png"/></a><a href="/strict/index.html"><img class="wbp" alt="thumb3" src="img/thumb3.png"/></a></div>
                <div class="slide">
                        <img class="wbp" alt="thumb4" src="img/thumb4.png"/><img class="wbp" alt="thumb5" src="img/thumb5.png"/><img class="wbp" alt="thumb6" src="img/thumb6.png"/></div>
                <div class="slide">
                        <img class="wbp" alt="thumb7" src="img/thumb7.png"/><img class="wbp" alt="thumb8" src="img/thumb8.png"/><img class="wbp" alt="thumb9" src="img/thumb9.png"/></div>
                     <div onclick="plusDivs(-1)" class="button previous_button">&#60;</div>
                    <div onclick="plusDivs(+1)" class="button next_button">&#62;</div>
                </div>

I want to make this javascript stop when the last slide is shown, so that the next button won't work. 我要在显示最后一张幻灯片时停止此javascript,以使下一个按钮不起作用。

For that you can add a new if statement to your showDivs function 为此,您可以向showDivs函数添加新的if语句

if (n > x.length) {slideIndex = x.length}

so your showDivs function should look like this now: 因此您的showDivs函数现在应如下所示:

function showDivs(n) {
    var i;
    var x = document.getElementsByClassName('slide');
    if (n > x.length) {slideIndex = x.length}
    if (n < 1) {slideIndex = 1;}
    for (i = 0; i < x.length; i++) {
        x[i].style.display = 'none';
    }
    x[slideIndex-1].style.display = 'block';
    }

but same with the previous button when the first slide is shown. 但与显示第一张幻灯片时的上一个按钮相同。 For the previous button, when I press it and the slider is at the first slide I want it to stay to the current slide. 对于上一个按钮,当我按下它并且滑块在第一张幻灯片上时,我希望它停留在当前幻灯片上。

This should be already working because thats the purpose of the following statemnt in your showDiv function: 这应该已经在工作,因为这就是showDiv函数中以下状态的目的:

if (n < 1) {slideIndex = 1;} 如果(n <1){slideIndex = 1;}

To achieve expected result, add below line to the showDivs function 要获得预期的结果,请在showDivs函数中添加以下行

//add this line to stop after last slide
  if(n > x.length){
      n= slideIndex =x.length;
   }

Codepen- https://codepen.io/nagasai/pen/vZVeZV Codepen- https: //codepen.io/nagasai/pen/vZVeZV

Solution: 解:

Above mentioned solution, will check whether the next value is more than the count of slide class(ie, 3 in this case) 上面提到的解决方案,将检查下一个值是否大于幻灯片类的计数(在这种情况下为3)

If it is more than the count of slide class, reset n and slideIndex back to the length of slide class. 如果大于幻灯片类的数量, 则将 nslideIndex重置为幻灯片类的长度。

For previous slide at first slide, there is already if condition which works 对于第一张幻灯片的上一张幻灯片,已经存在是否可以工作的条件

if (n < 1) {slideIndex = 1;}

you must check if value of slideIndex greater than length array of slides then assign this length to slideIndex. 您必须检查slideIndex的值是否大于幻灯片的长度数组,然后将此长度分配给slideIndex。 The same if value of slide index smaller than 1 then assign 1 to it. 如果幻灯片索引的值小于1,则将其赋值为1。

Simple code like bellow: 像下面这样的简单代码:

function plusDivs(n) {
    slideIndex += n;
    if(slideIndex >= x.length){slideIndex = x.length;}    
    if(slideIndex <=1){slideIndex=1}
    showDivs(slideIndex += n);
}

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

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