jQuery 如何获取旋转木马的总数和当前幻灯片数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17823571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:01:22  来源:igfitidea点击:

How to Get total and Current Slide Number of Carousel

jquerytwitter-bootstrapslidercarousel

提问by Mona Coder

Can you please let me know how I can get the total and current number of the carousel slides in bootstrap like the image below?

您能否让我知道如何获得引导程序中轮播幻灯片的总数和当前数量,如下图所示?

Image showing current page and total number of pages

显示当前页面和总页数的图像

I have an standard Bootstrap carousel and a <div>with the .numclass to display the total and current number and I used this code to retrieve the numbers but it didn't go through

我有一个标准的引导轮播和<div>.num类显示的总电流的号码,我用这个代码检索数字,但它并没有去通过

$('.num').html(){
  $('#myCarousel').carousel({number})
}

Thanks

谢谢

Update:

更新:

Please find a sample at this jsfiddle LINK

请在此 jsfiddle链接中找到示例

回答by Khawer Zeshan

Each slidehas a .itemclass to it, you can get the total number of slides like this

每个slide都有一个.item类,你可以得到这样的幻灯片总数

var totalItems = $('.item').length;

Active slidehas a class named as active, you can get the index of active slidelike this

Activeslide有一个名为 as 的类active,你可以active slide像这样获取索引

var currentIndex = $('div.active').index() + 1;

You can update these values by binding the bootstrap carousel slidevent like this

您可以通过slid像这样绑定引导轮播事件来更新这些值

$('#myCarousel').bind('slid', function() {
    currentIndex = $('div.active').index() + 1;
   $('.num').html(''+currentIndex+'/'+totalItems+'');
});

EXAMPLE

例子

回答by IqbalBary

Update of @Khawer Zeshan's code

For Bootstrap 3.0+ use slid.bs.carouselinstead of slidand oninstead of bind. So the update code will be like that

更新@Khawer Zeshan's code

For Bootstrap 3.0+ use slid.bs.carouselinstead of slidand oninstead of bind. 所以更新代码会是这样

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

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

回答by user3221264

Bootstrap 3.2+:

引导程序 3.2+:

carouselData.getItemIndex(carouselData.$element.find('.item.active'))

回答by Mithun Billara

var totalItemsPop = $('#Mycarousel .item').length; 
$('#Mycarousel').on('slide.bs.carousel', function() {
       setTimeout(function(){ 
            currentIndexPop = $('#Mycarousel div.active').index() + 1;
            $('.num').html('' + currentIndexPop + '/' + totalItemsPop + '');
         }, 1000);
   });

after slide event, div will be active and can not get active index, so keep your code inside set time out function

滑动事件后,div 将处于活动状态并且无法获得活动索引,因此请将您的代码保留在设置超时功能中

回答by ali alasady

Bootstrap 4 and js

Bootstrap 4 和 js

  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>

回答by Syed

The only drawback with the below code will be that it doesn't show up till you go to next slide (triggers after each slide change).

下面代码的唯一缺点是它在您转到下一张幻灯片之前不会显示(在每次幻灯片更改后触发)。

JS/jQuery

JS/jQuery

$('#carousel-slide').on('slid.bs.carousel', function () {
  var carouselData = $(this).data('bs.carousel');
  var currentIndex = carouselData.getActiveIndex();
  var total = carouselData.$items.length;

  var text = (currentIndex + 1) + " of " + total;
  $('#carousel-index').text(text);
});

HTML

HTML

<div id="carousel-index">
  <!-- Number will be added through JS over here -->
</div>

回答by M Kaweepatt Churcharoen

You can use jquery index() function to get the current index of active element inside list of item. So the code is look like this:

您可以使用 jquery index() 函数来获取项目列表中活动元素的当前索引。所以代码看起来像这样:

var currentItem = $("#carousel-1 .item.active" );
var currentIndex = $('#carousel-1 .item').index(currentItem) + 1;