繁体   English   中英

将“活动”类添加到 URL 哈希导航按钮

[英]Add 'Active' class to URL Hash Navigation button

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

如何向按钮添加“活动”类?

当前按钮有一个 button.secondary:hover & button.secondary:focus 来创建按钮背景颜色变化。

这很好,除非每当单击滑块中的任意位置时焦点都会发生变化,因此按钮的背景颜色会被移除。

我需要按钮具有专用的活动状态,以便可以在滑块内单击。

谢谢

您可以使用 javascript 或 jQuery 解决此问题,这是一个 jQuery 示例:

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

当您单击具有“按钮”类的任何元素并将其添加到您单击的元素时,这将删除所有按钮的“活动”类。

玩了半天,终于想到了解决办法。

  1. 在哈希导航中添加 #link 作为类,在这种情况下添加一个额外的类“slidetabs”,例如<a class="night_tubing slidetabs" href="#night_tubing">NIGHT TUBING</a>
  2. 获取当前幻灯片项,然后获取其数据哈希
  3. 将该数据散列作为一个类输入到更改的函数中,以便将类添加到导航按钮
  4. 然后对更改事件重复相同的操作以删除活动类
  5. 将 css 添加到 .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'); });

注意:这是按预期工作的,所以我没有进一步研究它,可能有更好的解决方案和良好的编码。

以下是完整代码:

滑块 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM