简体   繁体   中英

Is there a way to get current slide number of an auto-slide w3 carousel?

https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_carousel_interval&stacked=h

I have tried using the code below

function get_current_slideNumber(){
$('#myCarousel').on('slide.bs.carousel', function() {
    currentIndex = $('div.active').index() + 1;
   $('.num').html(''+currentIndex+'/'+totalItems+'');
});
}

What is the solution to this problem? Please provide a demo if possible.

use Bootstrap with javascript

 var totalItems = $('.item').length; var currentIndex = $('div.item.active').index() + 1; var down_index; $('.num').html(''+currentIndex+'/'+totalItems+''); $(".next").click(function(){ currentIndex_active = $('div.item.active').index() + 2; if (totalItems >= currentIndex_active) { down_index= $('div.item.active').index() + 2; $('.num').html(''+currentIndex_active+'/'+totalItems+''); } }); $(".prev").click(function(){ down_index=down_index-1; if (down_index >= 1 ) { $('.num').html(''+down_index+'/'+totalItems+''); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="num"></div> <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-wrap="false" data-interval="false"> <ol class="carousel-indicators"> <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li> <li data-target="#carouselExampleIndicators" data-slide-to="1"></li> <li data-target="#carouselExampleIndicators" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <div class="carousel-item item active"> <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="First slide"> </div> <div class="carousel-item item"> <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Second slide"> </div> <div class="carousel-item item"> <img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Third slide"> </div> </div> <a class="carousel-control-prev prev" href="#carouselExampleIndicators" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next next" href="#carouselExampleIndicators" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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