【问题标题】:owl carousel jump to specific slide and add class to itowl carousel 跳转到特定幻灯片并为其添加类
【发布时间】:2018-10-02 08:12:53
【问题描述】:

我使用 Owl Carousel 创建了一个带有帖子的基于幻灯片的页面。我为它实现了数据哈希,它工作得很好,除了我需要一个向当前哈希项添加一个类的函数,所以当改变哈希位置或接收外部哈希链接时,这个项目添加一个类。

这是我的代码,我使用 Owl Carousel v2.3.4 并且网站在 wordpress 中:

$('#post_top_slider').owlCarousel({
  loop: false,
  margin: 0,
  nav: true,
  dots: false,
  URLhashListener: true,
  startPosition: 'URLHash',
  addClassActive: true,
  autoplayHoverPause: true,
  responsive: {
    0: {
      items: 1
    },
    600: {
      items: 2
    },
    1000: {
      items: 3
    }
  }
});

$('#post_top_slider .owl-stage .owl-item').first().addClass('ps_actve');

$('#post_top_slider .owl-stage .owl-item').click(function(event) {
  $(this).siblings('.owl-item').removeClass('ps_actve');
  $(this).addClass('ps_actve');
  var tab_id = $(this).children('.pts_item').attr('data-id');
  //alert(tab_id);
  $(tab_id).siblings('.ptsd_item').hide();
  $(tab_id).show();
});

我需要添加到哈希项的类是“ps_actve”。网址是here

【问题讨论】:

  • 这与owl无关!

标签: jquery owl-carousel


【解决方案1】:

问题消失了。我只是将代码放在 owlcarousel.js 中,第 3307 行:

current.siblings('.owl-item').removeClass('ps_actve');
current.addClass('ps_actve');
var tab_id = current.children('.pts_item').attr('data-id');
//alert(tab_id);
$(tab_id).siblings('.ptsd_item').hide();
$(tab_id).show();

【讨论】:

  • 不推荐修改库。如果您更新包,更改很容易丢失,并且如果您使用 NPM 或类似方法,您的更改将不会传播给其他开发人员。相反,在您自己的代码中使用事件挂钩。
  • 这就是我试图做的,但我无法弄清楚。我尝试使用 hashchange.owl.carousel 事件,但它不起作用..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
相关资源
最近更新 更多