gpt4 book ai didi

javascript - 获取总数和当前幻灯片编号 bootstrap Slider

转载 作者:行者123 更新时间:2023-11-28 16:57:46 25 4
gpt4 key购买 nike

通过 JavaScript 和 bootstrap 控件了解幻灯片数量的最简单方法。按下按钮时,计数器递增或递减,标准 Bootstrap 轮播和带有 .num 类的 .num 类显示总数和当前数量,我使用此代码检索号码但未通过

 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">

<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>

最佳答案

正确的 jQuery 选择器应该是 .carousel-item 而不是 .item...

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

var down_index;
$('.num').html(''+currentIndex+'/'+totalItems+'');

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

https://www.codeply.com/go/m3vwD5Ackb

关于javascript - 获取总数和当前幻灯片编号 bootstrap Slider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55495194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com