javascript - 猫头鹰轮播定位div

标签 javascript jquery html css owl-carousel-2

我正在使用 owl carousel 2,我正在使用中心功能。我有 5 个元素要显示在轮播上。是否可以使用 jquery 添加类以在轮播中查找某些 div,而不必在 div 上添加 id 或类。 http://owlcarousel.owlgraphic.com/demos/center.html

这是我的html

<div class="owl-carousel">
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
</div>

这是我的js

$(document).ready(function () {
$(".owl-carousel").owlCarousel({
    center: true,
    items:5,
    loop:true,
    margin:0,
    responsive:{
        600:{
            items:5
        }
    },
    nav:true

});


});

如果您要在现场观看实际的轮播,我想要的是添加这两个类“a”和“b”

<div class="owl-carousel">
  <div class="product"> Your Content </div>
  <div class="product b"> Your Content </div>
  <div class="product a"> Your Content </div>
  <div class="product center"> Your Content </div>
  <div class="product a"> Your Content </div>
  <div class="product b"> Your Content </div>
  <div class="product"> Your Content </div>
</div>

所以我基本上是在尝试定位中心旁边的 div 和中心旁边的第二个 div

最佳答案

好了,1 分钟试用:

$("#owlSlider").owlCarousel({
  navigation : false,
  autoPlay:true,
  afterMove : function (elem) {
    var lng = $('.product').length,
        a = [],
        b = [];
    if(!!$('.center').length) {
      $('.a, .b').removeClass('a b');
      a.push($('.center').index()-1>=0?$('.center').index()-1:lng-1);
      a.push($('.center').index()+1<lng?$('.center').index()+1:0);
      b.push($('.center').index()-2>=0?$('.center').index()-2:lng-2);
      b.push($('.center').index()+2<lng?$('.center').index()+2:$('.center').index()+2-lng);
      a.forEach(function(item){
        $('.product:eq('+item+')').addClass('a');
      });
      b.forEach(function(item){
        $('.product:eq('+item+')').addClass('b');
      });
    }
  }
});

住在这里:http://codepen.io/raduchiriac/pen/doLQdg?editors=101

下一步是稍微优化一下,因为它非常非常难看。

关于javascript - 猫头鹰轮播定位div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011132/

相关文章:

javascript - 如何将 express 和 node 设置到现有的仅前端 Angular 2 项目中?

javascript - 获取url参数: JavaScript not working if used third bracket in the name attribute of input filed

javascript - 单击按钮时显示和隐藏菜单,并通过外部单击隐藏菜单

javascript - 如何在 Javascript 乒乓球游戏中正确地偏转球?

javascript - 光滑的 slider 拇指不显示

javascript - Angular 4 RxJs Observable Subjects 数组未使用新对象更新

javascript - ContentEditable 图像在 chrome 中调整大小,什么是最好的解决方案?

javascript - 将边框半径包含在 javascript 中时不起作用

javascript - 在 div 中播放音频

javascript - Bootstrap 验证器 : How do you add and remove validators dynamically to an existing input field?