How can I add class to the first and last item among the visible items of Owl Carousel 2? (ok)

dev.vieclam123.vn Toic

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="owlcarousel.css">
    
  </head>

  <body>
    <div class="latest-work-carousel">
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
  </div>
  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  
  <script src="owlcarousel.js"></script>
  <script src="script.js"></script>
    
  </body>

</html>
// Code goes here


jQuery(document).ready(function($) {
    
    var carousel = $(".latest-work-carousel");
    carousel.owlCarousel({
        loop : true,
        items : 3,
        margin:0,
        nav : true,
        dots : false,
    });

    checkClasses();
    carousel.on('translated.owl.carousel', function(event) {
        checkClasses();
    });

    function checkClasses(){
        var total = $('.latest-work-carousel .owl-stage .owl-item.active').length;
        
        $('.latest-work-carousel .owl-stage .owl-item').removeClass('firstActiveItem lastActiveItem');
        
        $('.latest-work-carousel .owl-stage .owl-item.active').each(function(index){
            if (index === 0) {
                // this is the first one
                $(this).addClass('firstActiveItem');
            }
            if (index === total - 1 && total>1) {
                // this is the last one
                $(this).addClass('lastActiveItem');
            }
        });
    }

        
});

/* Styles go here */

.firstActiveItem {
  background: red;
}

.lastActiveItem {
  background: blue;
}

Last updated