引导轮播最后一项将隐藏正确的控件

时间:2018-10-22 00:52:16

标签: jquery bootstrap-4 bootstrap-carousel

我正在使用bootstrap 4.0版本。我想在第一个项目上隐藏左控件,并在最后一个项目上隐藏右控件。 我认为使用jQuery可以解决此问题。

左侧控件将一直隐藏,仅在最后一项显示。右控制将一直显示,但隐藏在最后一个项目中。

HTML:

<div id="carousel-1" class="carousel slide" data-ride="carousel">

      <div class="carousel-inner">
        <div class="carousel-item active">
          Slide 1
        </div>
        <div class="carousel-item">
          Slide 2
        </div>
        <div class="carousel-item">
          Slide 3
        </div>
      </div>
      <a class="carousel-control-prev" href="#carousel-1" 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" href="#carousel-1" 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').carousel({
    interval: false,
})

function checkitem()
{
    var $this = $('#carousel-1');
    if ($('.carousel-inner .carousel-item:first').hasClass('active')) {
        $this.children('.carousel-control-prev').hide();
    } else if ($('.carousel-inner .carousel-item:last').hasClass('active')) {
        $this.children('.carousel-control-next').hide();
    } else {
        $this.children('.carousel-control').show();

    }
}

3 个答案:

答案 0 :(得分:1)

您可以使用data-wrap="false"属性停止从最后一项或第一项前进或后退。

$(document).ready(function(){
  // When strating hide prev arrow
  $('.carousel-control-prev').hide();
});



$('#carousel-1').on('slide.bs.carousel', function (e) {

  var slidingItemsAsIndex = $('.carousel-item').length - 1;

  // If last item hide next arrow
  if($(e.relatedTarget).index() == slidingItemsAsIndex ){
      $('.carousel-control-next').hide();
  }
  else{
      $('.carousel-control-next').show();
  }

  // If first item hide prev arrow
  if($(e.relatedTarget).index() == 0){
      $('.carousel-control-prev').hide();
  }
  else{
      $('.carousel-control-prev').show();
  }

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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 id="carousel-1" class="carousel slide" data-ride="carousel" data-wrap="false">

      <div class="carousel-inner">
        <div class="carousel-item active">
          <img width="900" src="https://cdn.mos.cms.futurecdn.net/FUE7XiFApEqWZQ85wYcAfM-970-80.jpg"/>
        </div>
        <div class="carousel-item">
           <img width="900" src=" https://images.unsplash.com/photo-1532274402911-5a369e4c4bb5?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=81a5f1725ca68c549e0054dcfdf269de&auto=format&fit=crop&w=1350&q=80"/>
        
        </div>
        <div  class="carousel-item">
           <img width="900" src="https://cdn.mos.cms.futurecdn.net/3YKAsCxSpoqFUEHyrQyfLb-650-80.jpg"/>
        </div>
      </div>
      <a class="carousel-control-prev" href="#carousel-1" 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" href="#carousel-1" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

答案 1 :(得分:1)

我删除了UPDATE,使其无法自动启动,并设置了CASE WHENdata-ride="carousel"
interval: false中添加了warap: falseBootstrap class),使其开始隐藏。

如果至少有两个项目,则显示.d-none控件。
然后,在每个slide上检查下一个位置并相应显示control

next
control
var carouselLength = $('.carousel-item').length - 1;

// If there is more than one item
if (carouselLength) {
    $('.carousel-control-next').removeClass('d-none');
}

$('.carousel').carousel({
    interval: false,
    wrap: false
}).on('slide.bs.carousel', function (e) {
    // First one
    if (e.to == 0) {
        $('.carousel-control-prev').addClass('d-none');
        $('.carousel-control-next').removeClass('d-none');
    } // Last one
    else if (e.to == carouselLength) {
        $('.carousel-control-prev').removeClass('d-none');
        $('.carousel-control-next').addClass('d-none');
    } // The rest
    else {
        $('.carousel-control-prev').removeClass('d-none');
        $('.carousel-control-next').removeClass('d-none');
    }
});

答案 2 :(得分:0)

如果您使用data-ride =“ false”,这是一种通用解决方案:

 $('.carousel[data-ride="false"]').each(function(){
    $(this).find('.carousel-control-prev').hide();
    $(this).on('slide.bs.carousel', function (e) {
        var slidingItemsAsIndex = $(this).find('.carousel-item').length - 1;
        // If last item hide next arrow
        if($(e.relatedTarget).index() == slidingItemsAsIndex ){
            $(this).find('.carousel-control-next').hide();
        }
        else{
            $(this).find('.carousel-control-next').show();
        }
        if($(e.relatedTarget).index() == 0){
            $(this).find('.carousel-control-prev').hide();
        }
        else{
            $(this).find('.carousel-control-prev').show();
        }
    });
});