简体   繁体   English

轮播编号计数器重置

[英]Carousel number counter reset

I have created a bootstrap carousel and am now trying to create a number counter that shows the total number of pages and the current page number.我创建了一个引导轮播,现在正在尝试创建一个数字计数器,显示总页数和当前页码。

It works fine until the carousel starts again and switches to the first page.它工作正常,直到轮播再次启动并切换到第一页。 Instead of going from "Last slide/Total slides" to "First slide/Total slide" it still showing "Last slide/Total slides" on the first slide.而不是从“最后一张幻灯片/总幻灯片”到“第一张幻灯片/总幻灯片”,它仍然在第一张幻灯片上显示“最后一张幻灯片/总幻灯片”。 But it resets on the second slide.但它会在第二张幻灯片上重置。

Another problem that I think is related to the above is if it counts backwards from the first page to the last page it continues to show "First page/Total slides" until I click the next button.我认为与上述相关的另一个问题是,如果它从第一页倒数到最后一页,它会继续显示“第一页/总幻灯片”,直到我单击下一个按钮。

The link to the page (The first carousel)页面链接(第一个轮播)

/R /R

My HTML我的 HTML

<div id="nav-01" class="carousel" data-ride="carousel" data-interval="false">           
            
    <div class="carousel-inner noselect">
    <div class="carousel-item active item-01">
        
        <img class="case-img" src="../media.officeofpossibilities.se/public_html/1_Silent_moto_OP_web_Landscape_1920x1080.jpg" alt="Slide 01">
                            
    </div> <!--carousel-item active-->
                            
    <div class="carousel-item item-01">
                            
        <img class="case-img" src="../media.officeofpossibilities.se/public_html/2_OP_silent_moto_Sketch_web_Landscape_1920x1080-Recovered.jpg" alt="Slide 01">
                                                                            
    </div>
                       
    <div class="carousel-item item-01">
                                        
        <img class="case-img" src="../media.officeofpossibilities.se/public_html/4_Diagram_Silent_moto_OP_web_Landscape_1920x1080-1.jpg" alt="Slide 01">
                            
    </div>              
                    
    <div class="carousel-title">    
        <a class="prev-01" href="#nav-01" role="button" data-slide="prev">Back</a>
        <a class="next-01" href="#nav-01" role="button" data-slide="next">Next</a>
    </div>
                            
    <div class="num-01"></div>

    </div>
</div>


JS: JS:

  var totalItems = $('.item-01').length;
            var currentIndex = $('div.carousel-item').index() + 1;

            var down_index;
            $('.num-01').html(''+currentIndex+'&nbsp;/&nbsp;'+totalItems+'');

                $(".next-01").click(function(){
                currentIndex_active = $('div.carousel-item.active').index() + 2;
                if (totalItems >= currentIndex_active)
                {
                    down_index= $('div.carousel-item.active').index() + 2;
                    $('.num-01').html(''+currentIndex_active+'&nbsp;/&nbsp;'+totalItems+'');
                }
            });

                $(".prev-01").click(function(){
                    down_index=down_index-1;
                if (down_index >= 1 )
                {
                    $('.num-01').html(''+down_index+'&nbsp;/&nbsp;'+totalItems+'');
                }
            });

In your current jquery code as you go to last slide ie : 3 the currentIndex_active will be 3 and when you click next button again its value become 4 and there is no such slide so if-statement will not get executed and that's the reason no value change .Instead you can add an else part to refresh its values and just show $('.num-01').html('1' + '&nbsp;/&nbsp;' + totalItems + '');在您当前的 jquery 代码中,当您转到最后一张幻灯片时,即: 3 currentIndex_active将为3 ,当您再次单击下一步按钮时,它的值变为4并且没有这样的幻灯片,因此不会执行if-statement ,这就是没有值的原因更改 .相反,您可以添加一个else部分来刷新其值并只显示$('.num-01').html('1' + '&nbsp;/&nbsp;' + totalItems + ''); and same for back button you can do.和你可以做的后退按钮一样。

Demo Code :演示代码

 var totalItems = $('.item-01').length; var currentIndex = $('div.carousel-item').index() + 1; var down_index; $('.num-01').html('' + currentIndex + '&nbsp;/&nbsp;' + totalItems + ''); $(".next-01").click(function() { currentIndex_active = $('div.carousel-item.active').index() + 2; if (totalItems >= currentIndex_active) { down_index = $('div.carousel-item.active').index() + 2; $('.num-01').html('' + currentIndex_active + '&nbsp;/&nbsp;' + totalItems + ''); } else { down_index = 1; //just to make 0 to go to else part when back button is clicked $('.num-01').html('1' + '&nbsp;/&nbsp;' + totalItems + ''); } }); $(".prev-01").click(function() { down_index = down_index - 1; if (down_index >= 1) { $('.num-01').html('' + down_index + '&nbsp;/&nbsp;' + totalItems + ''); } else { down_index = totalItems; //last slide value $('.num-01').html('' + totalItems + '&nbsp;/&nbsp;' + totalItems + ''); } });
 img { height: 100px; width: 100px; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <div id="nav-01" class="carousel slide" data-ride="carousel" data-interval="false"> <div class="carousel-inner noselect"> <div class="carousel-item active item-01"> <img class="case-img" src="http://placehold.it/1200x600/555/000&text=One" alt="Slide 01"> </div> <!--carousel-item active--> <div class="carousel-item item-01"> <img class="case-img" src="http://placehold.it/1200x600/555/000&text=two" alt="Slide 01"> </div> <div class="carousel-item item-01"> <img class="case-img" src="http://placehold.it/1200x600/fcf00c/000&text=Three" alt="Slide 01"> </div> </div> <div class="carousel-title"> <a class="prev-01" href="#nav-01" role="button" data-slide="prev">Back</a> <a class="next-01" href="#nav-01" role="button" data-slide="next">Next</a> </div> <div class="num-01"></div> </div>

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

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