Bootstrap Carousel jQuery第1个滑块编号(共5个)直到单击才更新



在我的Bootstrap 4旋转木马下面,我有一个指示器,上面写着当前幻灯片的编号和滑块的总数,所以是5的1,5的2,5的3,等等。

使用Bootstrap挂钩,我的计数器应该在点击下一张幻灯片时递增,它确实如此,但直到点击了两张幻灯片,所以在更新到5张幻灯片中的2张之前,它将在两张幻灯片中保持在5张幻灯片的1张,并且功能正常,但因为它没有启动,所以顺序不正确:

第一:1/5第二:1/5第三:2/5第四次:3/5第五:4/5第一次(再次(:5/5

然而,它应该从1/5开始,然后点击,根据实际幻灯片编号转到2/5,然后再转到3/5等。

这是我的jQuery代码

jQuery(function($) {
var totalItems = $('.carousel-item').length;
var currentIndex = $('div.active').index() + 1;
$('#carousel-number').html('<p>'+currentIndex+' of '+totalItems+'</p>');
$('#brands-carousel').on('slide.bs.carousel', function () {
currentIndex = $('div.active').index() + 1;
$('#carousel-number').html('<p>'+currentIndex+' of '+totalItems+'</p>');
});
});

当索引从0开始时,我向它添加了+1。(第0个,共5个,而不是第1个(

还有我的HTML/PHP旋转木马

<?php
if( have_posts() ) :
$count = 0;
$counti = 0;
$content = apply_filters( 'the_content', get_the_content() );
?>
<div class="brands-carousel">
<div id="brands-carousel" class="carousel slide" data-ride="carousel" data-interval="false">
<div class="carousel-navigation">
<ol class="carousel-indicators" id="carousel-indicators">
<?php
while( have_posts()) : the_post(); ?>
<li id="carousel-number" data-target="#brands-carousel" data-slide-to="<?php echo $counti; ?>" class="<?php echo ($counti == 0 ? 'active' : 'hide'); ?>">
</li>
<?php
endwhile;
?>
</ol>
<a class="carousel-control-prev" data-target="#brands-carousel" 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" data-target="#brands-carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<div class="carousel-inner" role="listbox">
<?php
while(have_posts()) : the_post();
?>
<div class="carousel-item <?php echo ($count == 0 ? 'active' : '') ?>">
<div id="content">
<div class="row different-row">
<div class="col-6 the-difference">
<h1>The Difference</h1>
<p><?php the_title(); ?></p>
</div>
<div class="col-6 the-benefit">
<h1>The benefit to you</h1>
<div class="benefit-wrap">
<?php echo apply_filters( 'the_content', get_the_content() ); ?>
</div>
</div>
</div>
</div>
</div>
<?php
$count++;
endwhile;
?>
</div>
</div>
</div>
<?php
endif;
?>

如有任何帮助,将不胜感激

请删除data-interval="false"或将其从您的html部分中设为true,然后了解我。

最新更新