Search code examples
javascriptjquerycssowl-carousel

Owl Carousel, navigation disabled after reaching first/last item


I'm trying to use owl carousel for my website. I want to disable the navigation after it reach first/last item, for example by adding "disabled" class in navigation then disable it via css. Is it possible?

my code

$(document).ready(function() {
  var owl = $("#owl-demo");
  owl.owlCarousel({
    rewindNav : false,	
    pagination : false,        
    items : 4
  });
  // Custom Navigation Events
  $(".next").click(function(){
    owl.trigger('owl.next');
  })
  $(".prev").click(function(){
    owl.trigger('owl.prev');
  })
});
.item { background: #e5e5e5; margin: 10px}
.btn { background: #bd0000; color: #fff; padding: 5px 10px; cursor: pointer}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://owlgraphic.com/owlcarousel/owl-carousel/owl.carousel.js"></script>
<link href="http://owlgraphic.com/owlcarousel/owl-carousel/owl.carousel.css" rel="stylesheet" />

<div id="owl-demo" class="owl-carousel owl-theme">
  <div class="item"><h1>1</h1></div>
  <div class="item"><h1>2</h1></div>
  <div class="item"><h1>3</h1></div>
  <div class="item"><h1>4</h1></div>
  <div class="item"><h1>5</h1></div>
  <div class="item"><h1>6</h1></div>
  <div class="item"><h1>7</h1></div>
  <div class="item"><h1>8</h1></div>
  <div class="item"><h1>9</h1></div>
  <div class="item"><h1>10</h1></div>
</div>

<div class="customNavigation">
  <a class="btn prev">Previous</a>
  <a class="btn next">Next</a>
</div>

http://jsfiddle.net/p3d52z4n/1/


Solution

  • You can use callbak afterAction and with your custom controls

    afterAction: function(){
      if ( this.itemsAmount > this.visibleItems.length ) {
        $('.next').show();
        $('.prev').show();
    
        $('.next').removeClass('disabled');
        $('.prev').removeClass('disabled');
        if ( this.currentItem == 0 ) {
          $('.prev').addClass('disabled');
        }
        if ( this.currentItem == this.maximumItem ) {
          $('.next').addClass('disabled');
        }
    
      } else {
        $('.next').hide();
        $('.prev').hide();
      }
    }
    

    Check the working demo - http://jsfiddle.net/p3d52z4n/9/