0

I'm trying to add an inactive class for the first and last item.

My HTML code :

<div id="owl-nav">
    <div class="owl-carousel">
        <div class="item">text</div>
    </div>
</div>

Thanks for your help.

1 Answers1

1

Edit: The following changes should allow you to use the callback to dynamically grab the number of items and target the first and last instance of the "items".

Perhaps use the Callback data option to access the image index position, and if it is 0 or whatever number the last child is (the number of 0 indexed items in your carousel), add the class "inactive" to the element using the addClass/removeClass methods.

http://www.owlcarousel.owlgraphic.com/docs/api-events.html

$('.owl-carousel').owlCarousel({
    onChange: callback
});

function callback(event) {
var element   = event.target;   // DOM element, in this example .owl-carousel
var items     = event.item.count;     // Number of items
var itempos      = event.item.index;     // Position of the current item
var firstchild = $('.item:nth-child(1)'); 
var lastchild = $('.item:last-child');
    if(item === 0){
      firstchild.addClass("inactive");
    }else if(itempos  === items){
      lastchild.addClass("inactive");
    }
}
Korgrue
  • 3,430
  • 1
  • 13
  • 20
  • The callback has an option that will return the number of elements automatically for you - regardless if it changes. Look under callback Data elements on the page I linked you. Hope this helps. – Korgrue Jan 15 '16 at 19:58
  • I updated my response for you. It includes a sample implementation. Since I dont have an active slider to test against, you may find that you have to make some changes to the script - but here is how the callback is implemented. – Korgrue Jan 15 '16 at 20:33