滑动猫头鹰轮播控件

时间:2015-11-17 13:08:56

标签: javascript jquery css owl-carousel

我在我的网站上使用owl carousel滑块。

在滑块中有更多控件(请参阅以黑色选择的图像)。

enter image description here

  

代码:

$("#owl-demo-2").owlCarousel({
  navigation : false,
  slideSpeed : 300,
  paginationSpeed : 400,
  singleItem : true,
  pagination : true,
  items : 1,
  afterMove:function(){
    var length = this.owl.owlItems.length;
    var current_item = this.owl.currentItem;
    /*- need to work here -*/
  },
  afterInit:function(){
    var length = this.owl.owlItems.length;
    var current_item = this.owl.currentItem;
    for(var i = 5; i < length; i++ ) {
      $("#owl-demo-2 .owl-controls .owl-pagination .owl-page:eq("+ i +")").css('display', 'none');
    }
  }
});
<div id="owl-demo-2" class="owl-carousel">
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
  <div class="item"><img src="img/product-img-1.jpg"></div>
</div>

我需要在滑块中仅显示5个控件(包括活动滑块),而另一个控件需要显示滑块格式。

我如何实现这一目标,帮助我。

由于

2 个答案:

答案 0 :(得分:2)

这是一个使用最新版Owl的jsfiddle,你想要/需要的东西。

http://jsfiddle.net/zu1hvhua/3/

使用这个CSS,我将控件居中并隐藏溢出:

.owl-controls {
    width: 130px;
    height: 28px;
    overflow: hidden;
    left: 50%;
    transform: translateX(-50%);
    position: relative;
    display: inline-block;
    border: 4px solid black;
}
.owl-dots {
    height: 28px;
    width: 100vw;
    position: relative;
    left: 0;
    transition: left .3s ease;
}
.owl-dots .owl-dot {
    float: left;
}

这个JS改变了点的左边距,因此活动的边缘始终在视图中。看起来更复杂,我必须处理异常(朝向目的)。

var owl = $('.owl-carousel');
owl.owlCarousel({
    margin: 10,
    loop: true
});
owl.on('changed.owl.carousel', function(event) {
    var activeIndex =  $('.owl-dots>.active').index(), dots = $('.owl-dots .owl-dot');
    if (activeIndex > 2) {
        if (activeIndex < (dots.length - 2)) {
            left = '-' + (24 * (activeIndex -2 )) + 'px';
        } else {
            left = '-' + (24 * (dots.length - 5 )) + 'px';
        }
    } else {
        left = '0';
    }
    $('.owl-dots').css({'left': left});
});

代码背后的原理非常简单:我将.owl-controls设置为overflow: hidden;的小型查看窗口。

在它后面,我让.owl-dots拥有100%的设备屏幕宽度。在更改幻灯片时,我使用left更改点容器的位置,以确保活动点始终位于中间,除非我们处于第一张或最后两张幻灯片。

答案 1 :(得分:1)

尝试以下代码: -

        afterMove:function(elem){
            var length = len = this.owl.owlItems.length;
            var current_owl_id = "owl-demo";
            if(length > 5) {
                var current_item_position = this.owl.currentItem;
                var current_item = 1 + current_item_position;
                var slide_row = Math.ceil(current_item/4);
                var slide_row_start = ((slide_row * 4) - 4);
                if( slide_row_start == current_item_position) {
                    var before_position = current_item_position - 1;
                    var val = check_before_postion_css(current_owl_id, before_position);
                    if( val == "none") {
                        slide_row = slide_row - 1;
                    }
                }
                var slide_row_start = ((slide_row * 4) - 4);
                if( current_item != length) {
                    slide_row_start = (slide_row_start <= 0) ? 0 :slide_row_start;
                    scroll_slide(current_owl_id, slide_row_start, 5, length, current_item);
                }
            }

        }

还要在顶部添加这些功能并更改ID名称:

 function scroll_slide(id, start, end, length, current_item) {
    $("#"+id +" .owl-controls .owl-pagination .owl-page").css('display', 'none');
    if(start > 0) {
        start = parseInt(current_item / 4) * 4;
    }
    start = (start -  parseInt(current_item / 4));
    start = (start <= 0) ? 0 : start;
    for(var i = 0; i < end; i++ ) {
        $("#"+id +" .owl-controls .owl-pagination .owl-page:eq("+ start +")").css('display', 'inline-block');
        start++;
    }
}
function check_before_postion_css(id, position) {
    return $("#"+id +" .owl-controls .owl-pagination .owl-page:eq("+ position +")").css('display');
}