donglun1918 2018-11-08 12:03
浏览 74

Bootstrap Carousel jQuery 1 of 5滑块编号在点击之前不会更新

Below my Bootstrap 4 Carousel, I have an indicator saying the current slide number and the total amount of sliders, so 1 of 5, 2 of 5, 3 of 5, etc.

Using the Bootstrap hook, my counter should increment when the next slide is clicked, which it does, but not until two slides have been clicked so it will stay on 1 of 5 for two slides before updating to 2 of 5 and functionally normally, but because it doesn't start, the order is incorrect:

First: 1/5 Second: 1/5 Third: 2/5 Forth: 3/5 Fifth: 4/5 First(again): 5/5

Whereas it should start at 1/5 then on click, go to 2/5, then 3/5 etc according to the actual slide number.

Here is my jQuery code

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>');

    });
});

I added + 1 to the index as it started at 0. (0 of 5 instead of being 1 of 5)

And my HTML / PHP Carousel

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

Any help would be greatly appreciated

  • 写回答

1条回答 默认 最新

  • dongsi4547 2018-11-08 12:55
    关注

    Please remove data-interval="false" or make it true from your html section then know me.

    评论

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑