简体   繁体   English

Bootstrap轮播下一个上一个按钮控制

[英]Bootstrap carousel next prev button control

I have a multiple items carousel. 我有多个物品轮播。 How can I hide the left control if the carousel is on the first item, and how can I hide the right control when the carousel is on the last item. 如果转盘位于第一个项目上,如何隐藏左控件;当转盘位于最后一个项目上时,如何隐藏右控件。 I've tried codes below but it doesn't work. 我已经尝试过下面的代码,但是它不起作用。

  $('#kesfetCarousel').on('slid', '', checkitem);
  checkitem();

   function checkitem()
    {
        if($('.carousel-inner .item:first').hasClass('active')) {
            $this.children('.left').hide();
        } else if($('.carousel-inner .item:last').hasClass('active')) {
            $this.children('.right').hide();
        } else {
            $this.children('.carousel-control').show();
        }
    }

 <div class="categoryBlock col-md-12 carousel slide" id="kesfetCarousel" data-ride="carousel">
<div class="subject">
    <div class="nextPrev">
        <div class="next"><a class="right carousel-control" href="#kesfetCarousel" role="button" data-slide="next"><i class="icon-rigthArrow2Black"></i> Next</a></div>
        <div class="prev"><a class="left carousel-control" href="#kesfetCarousel" role="button" data-slide="prev"><i class="icon-leftArrowBlack2"></i> Prev</a></div>
    </div>
</div>
<div class="carousel-inner" role="listbox">
    <div class="carousel-inner" role="listbox">
        <div class="categoryList col-md-12 item">
            <div class="categoryContent col-md-4">
                <img width="300" height="200" src="http://cdn.yemek.com/wp-content/uploads/2015/08/hamursuz-pizza-tarifi-720x450.jpg"></a>
            </div>
            <div class="categoryContent col-md-4">
                <img width="300" height="200" src="http://cdn.yemek.com/wp-content/uploads/2015/08/hamursuz-pizza-tarifi-720x450.jpg"></a>
            </div>
        </div>                                              
    </div>
</div>
<div class="carousel-inner" role="listbox">
    <div class="carousel-inner" role="listbox">
        <div class="categoryList col-md-12 item">
            <div class="categoryContent col-md-4">
                <img width="300" height="200" src="http://cdn.yemek.com/wp-content/uploads/2015/08/hamursuz-pizza-tarifi-720x450.jpg"></a>
            </div>
            <div class="categoryContent col-md-4">
                <img width="300" height="200" src="http://cdn.yemek.com/wp-content/uploads/2015/08/hamursuz-pizza-tarifi-720x450.jpg"></a>
            </div>
        </div>                                              
    </div>
</div>

You are on the right track. 您走在正确的轨道上。 Your code doesn't work, because $this is not defined. 您的代码无效,因为未定义$this This has to be $(this) . 这必须是$(this)

Secondly, the slid event should be named slid.bs.carousel . 其次, slid的事件应该被命名为slid.bs.carousel

Thirdly, your if-statement will not work correctly. 第三,您的if陈述将无法正常运作。 Assume that the right control is hidden and assume that the carousel continues to the first slide. 假定隐藏了右边的控件,并假定轮播继续到第一张幻灯片。 Your code will hide the left control, but the right control remains hidden. 您的代码将隐藏左侧控件,但右侧控件仍处于隐藏状态。

So, you need a separate if-else to either hide or show the left control. 因此,您需要单独的if-else来隐藏或显示左控件。 And another if-statement for the right control. 另一个if语句用于正确控制。

Your code may look like: 您的代码可能如下所示:

$('#kesfetCarousel').on('slid.bs.carousel', checkitem);
checkitem();

function checkitem()
{
    if($('.carousel-inner .item:first').hasClass('active')) {
        $(this).children('.left.carousel-control').hide();
    } else {
        $(this).children('.left.carousel-control').show();
    }

    if($('.carousel-inner .item:last').hasClass('active')) {
        $(this).children('.right.carousel-control').hide();
    } else {
        $(this).children('.right.carousel-control').show();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM