bootstrap carousel hide controls on first and last

前端 未结 4 2286
长情又很酷
长情又很酷 2020-12-06 13:51

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.

My code below hides

4条回答
  •  不知归路
    2020-12-06 14:29

    Bootply link

    $('#myCarousel').on('slid', '', checkitem);  // on caroussel move
    $('#myCarousel').on('slid.bs.carousel', '', checkitem); // on carousel move
    
    $(document).ready(function(){               // on document ready
        checkitem();
    });
    
    function checkitem()                        // check function
    {
        var $this = $('#myCarousel');
        if($('.carousel-inner .item:first').hasClass('active')) {
            $this.children('.left.carousel-control').hide();
            $this.children('.right.carousel-control').show();
        } else if($('.carousel-inner .item:last').hasClass('active')) {
            $this.children('.left.carousel-control').show();
            $this.children('.right.carousel-control').hide();
        } else {
            $this.children('.carousel-control').show();
        } 
    }
    

提交回复
热议问题