javascript - jQuery Bootstrap Carousel 根据当前幻灯片显示内容

标签 javascript jquery twitter-bootstrap

我正在使用 bootstrap jquery 轮播来滑动内容。我在轮播下面也有一些内容。此内容应根据显示的幻灯片隐藏/显示。

由于 bootstrap 在当前幻灯片上使用 active,所以我编写了下面的代码。 现在,当我使用箭头时,这可以工作。但是,如果我使用键盘箭头进行下一个和上一个,或者当幻灯片自动播放时,内容不会相应地显示/隐藏。

jQuery:

    $('#success-stories .carousel-control.left, #success-stories .carousel-control.right').click(function() {
        if ( $('#success-stories .item1').hasClass("active") ) {
            $('.success1').removeClass("active"); 
            $('.success2').addClass("active"); 
        }
        if ( $('#success-stories .item2').hasClass("active") ) {
            $('.success2').removeClass("active"); 
            $('.success1').addClass("active"); 
        }

    });

谢谢

最佳答案

有很多方法可以实现这一点。根据您运行的 bootstrap 版本,您可以使用 slidslideslide.bs.carousel

3 之前的 Bootstrap 版本

你可以尝试这个(取决于你运行的bootstrap版本),使用slid函数,它会检测slide事件何时完成:

$("#myCarousel").carousel()
 $("#myCarousel").bind("slid", function(){
    $currentActive = $("#myCarousel .active").attr('id');
       if($currentActive == "item1"){
           //then show something
       }else if(...){....}
  })

使用幻灯片和滑动事件,您可以找到当前幻灯片和下一张幻灯片/目标幻灯片,此解决方案尚未经过测试,但应该可以正常工作。

$('.carousel').on('slide',function(e){
    var slideFrom = $(this).find('.active').index();
    var slideTo = $(e.relatedTarget).index();
    if(slideTo == 1){
       //do something for item one, realise here i am working with indexes
    }
});

在 Bootstrap 3 中:

$('#myCarousel').on('slide.bs.carousel', function (e) {
  //according to the documentation this event is fired when the slide method is invoked

    var slideFrom = $(this).find('.active').index();
    var slideTo = $(e.relatedTarget).index();
    if(slideTo == 1){
       //do something for item one, realise here i am working with indexes
    }
})
<小时/>

更新

为什么不给你的成功内容的 children div id,而不是使用类来识别他们,而是使用他们的个人id。
例如:

 <div id="success-content">
                <div class="success1 active" id="successOne">
                    <h4 class="rounded-heading">Eleanor's Story</h4>
                    <p>
                        <span class="quote-start"></span>
                             Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. comes from a line in section 1.10.32.
                        <span class="quote-finish"></span>
                    </p>
                    <p>
                        Isobel Leeds)
                    </p>
                    <p>
                        All case studies are genuine photographs and un-retouched case studies of our own patients treated in our own clinics. (Reproduced with their consent)
                    </p>

                </div><!-- end success1 -->
                <div class="success2" id="successTwo">
                    <h4 class="rounded-heading">Melsor's Story</h4>
                    <p>
                        <span class="quote-start"></span>
                             Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. comes from a line in section 1.10.32.
                        <span class="quote-finish"></span>
                    </p>
                    <p>
                        Isobel Leeds)
                    </p>
                    <p>
                        All case studies are genuine photographs and un-retouched case studies of our own patients treated in our own clinics. (Reproduced with their consent)
                    </p>
                </div>              
            </div><!-- end success-content -->

        </div><!-- end success-stories -->

关于javascript - jQuery Bootstrap Carousel 根据当前幻灯片显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349019/

相关文章:

javascript - JQuery 函数submit(),用于当用户在 input=text 中按 Enter 时,还提交位于同一表单中的 input=submit

javascript - 遇到 Async/Await Promise 问题 - Javascript/Nodejs

javascript - jquery 递增索引输入数组

html - bootstrap3 中的内联表单

jquery - Bootstrap 3 - 使用固定侧边栏时模态消失在背景下方

php - Bootstrap 导航栏上的帐户信息

javascript - 根据eslint配置保存时在atom编辑器中美化js代码

javascript - addClass 具有 find 的多个结果

javascript - 如何将无序列表放置在一个圆圈中?

javascript - 如何访问 Cypress 的 React 组件