Add 'Active' class to URL Hash Navigation button

2.8k views Asked by At

https://owlcarousel2.github.io/OwlCarousel2/demos/urlhashnav.html

How can I add 'active' class to the buttons?

Currently buttons have a button.secondary:hover & button.secondary:focus to create a button background color change.

This is fine, except whenever clicking anywhere in the slider the focus is changed and so the background color is removed from the button.

I need the buttons to have a dedicated active state, so that clicking within the slider is possible.

Thanks

2

There are 2 answers

0
duvigneau On

You can solve this with javascript or jQuery, here's a jQuery example:

$(document).ready(function() {
    $('.button').on('click', function(){
        $('.button').removeClass('active');
        $(this).addClass('active');
    });
});

This removes the 'active' class for all buttons when you click any element with the class 'button' and adds it to the one you're clicking.

0
Wasim Sayyed On

After playing around for a long time, I came up with the solution.

  1. Add #link as class in hash navigations, and one more additional class in this case "slidetabs" e.g. <a class="night_tubing slidetabs" href="#night_tubing">NIGHT TUBING</a>
  2. Get the current slide item and then its data-hash
  3. Feed that data hash as a class in on changed function for adding class to navigation button
  4. Then repeat same for change event to remove active class
  5. Add a css to .slidetabs.active

$('.owl-carousel').on('changed.owl.carousel', function(event) { var current = event.item.index; var hash = $(event.target).find(".owl-item").eq(current).find(".item").attr('data-hash'); $('.'+hash).addClass('active'); });

$('.owl-carousel').on('change.owl.carousel', function(event) { var current = event.item.index; var hash = $(event.target).find(".owl-item").eq(current).find(".item").attr('data-hash'); $('.'+hash).removeClass('active'); });

Note: this is works as expected so I have not worked any further on it, there may be better solutions with good coding out there.

following is the full code:

Slider HTML:

<div class="owl-carousel owl-theme owl-loaded owl-drag">
   <div class="owl-stage-outer">
      <div class="owl-stage">
         <div class="owl-item">
            <div class="item" data-hash="cowboy_coaster">
               <!-- Slide Content -->
            </div>
         </div>
         <div class="owl-item">
            <div class="item" data-hash="night_tubing">
               <!-- Slide Content -->
            </div>
         </div>
         <!-- . -->
         <!-- . -->
         <!-- . -->
         <!-- . -->
      </div>
   </div>
</div>
</div>
<div class="owl-nav">
   <button type="button" role="presentation" class="owl-prev">
      <img class="slider-arrow" src="/wp-content/uploads/2019/01/slider-arrow-pre.png">
      <div class="slider-counter">1 / 4</div>
   </button>
   <button type="button" role="presentation" class="owl-next"><img class="slider-arrow" src="/wp-content/uploads/2019/01/slider-arrow-next.png"></button>
</div>
<div class="owl-dots disabled"></div>
<div class="owl-slider-tabs">
   <ul>
      <li>
         <a class="dining slidetabs active" href="#dining">DINING</a>
      </li>
      <li>
         <a class="night_tubing slidetabs" href="#night_tubing">NIGHT TUBING</a>
      </li>
      <li>
         <a class="cowboy_coaster slidetabs" href="#cowboy_coaster">COWBOY COASTER</a>
      </li>
      <li>
         <a class="lift_tickets slidetabs" href="#lift_tickets">LIFT TICKETS</a>
      </li>
   </ul>
</div>

Javascript:

jQuery(document).ready(function($) {
  $('.owl-carousel').on('initialized.owl.carousel changed.owl.carousel', function(e) {
    if (!e.namespace)  {
      return;
    }
    var carousel = e.relatedTarget;
    $('.slider-counter').text(carousel.relative(carousel.current()) + 1 + ' / ' + carousel.items().length);
    }).owlCarousel({
      nav:true,
      navText: ["<img class='slider-arrow' src='/wp-content/uploads/2019/01/slider-arrow-pre.png'><div class='slider-counter'>1 / 3</div>","<img class='slider-arrow' src='/wp-content/uploads/2019/01/slider-arrow-next.png'>"],
      loop:true,
      slideSpeed : 300,
      paginationSpeed : 400,
      items:1,
      dots:false,
      URLhashListener:true,
      startPosition: 'URLHash',
      autoplay:true,
      autoplayTimeout:9000,
      autoplayHoverPause:true,
      animateOut: 'fadeOut',
      animateIn: 'fadeIn',
    responsiveClass:true,
    responsive:{
        0:{
            items:1,
            nav:true
        },
        600:{
            items:1,
            nav:true
        },
        1000:{
            items:1,
            nav:true
        }
    }
  });

    $('.owl-carousel').on('changed.owl.carousel', function(event) {
      var current = event.item.index;
      var hash = $(event.target).find(".owl-item").eq(current).find(".item").attr('data-hash');
      $('.'+hash).addClass('active');
    });

    $('.owl-carousel').on('change.owl.carousel', function(event) {
      var current = event.item.index;
      var hash = $(event.target).find(".owl-item").eq(current).find(".item").attr('data-hash');
      $('.'+hash).removeClass('active');
    });

});

CSS:

.slidetabs.active {
    color: #ce2d27;
    text-decoration: underline;
}