代码之家  ›  专栏  ›  技术社区  ›  mangrove108

如何访问carousel bootstrap4活动幻灯片以初始化事件

  •  0
  • mangrove108  · 技术社区  · 6 年前


    我知道如何在幻灯片更改(完成)后开始活动,如下所示: $('#myCarousel').on('slid.bs.carousel', function () {...}

    如下所述: Bootstrap Carousel 但是如何调用对象(方法) 只有 在第二张幻灯片上?

    2 回复  |  直到 6 年前
        1
  •  1
  •   bestestefan    6 年前

    这是易于编辑的解决方案

    let currentId = 0, 
        seekedId = 3;
    $('.carousel-item').each(function(){
        if(currentId == seekedId){
            $(this).on('slid.bs.carousel', function(){
                console.log('only on 3rd element');
            });
        }else{
            currentId += 1;
        }
    });
    

    $('.carousel-item:eq( 2 )').on('slid.bs.carousel', function(){
        console.log('3rd element do something');
    });
    
        2
  •  1
  •   mangrove108    6 年前

    经过来回的寻找,我发现我错过了 relatedTarget.id 所以我用这个switch语句来完成它,我相信上面的答案也是有办法的,但这对我来说很有效:

    $('#startPageCarousel').on('slide.bs.carousel', function (ev) {
      var id = ev.relatedTarget.id; // needed to get the id of the element
      switch (id) {
        case "3":
          // do something the id is 3
          var options = {
            useEasing: true,
            useGrouping: true,
            separator: ',',
            decimal: '.',
          };
          var stat1 = new CountUp('stats1', 0, numberOfPosts, 0, 5.5, options);
          var stat2 = new CountUp('stats2', 0, numberOfPosts, 0, 5.5, options);
          var stat3 = new CountUp('stats3', 0, numberOfPosts, 0, 5.5, options);
          var stat4 = new CountUp('stats4', 0, numberOfPosts, 0, 5.5, options);
    
          if (!stat1.error && !stat2.error && !stat3.error && !stat4.error) {
            stat1.start();
            stat2.start();
            stat3.start();
            stat4.start();
          }
          break;
      }
    });