简体   繁体   中英

Slide the Owl Carousel Controls

I am using owl carousel slider in my site.

In the slider have more controls (see the image selected in black color).

在此输入图像描述

CODE :

$("#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>

I need to show only 5 controls in the slider (include active slider) and the another controls need to show the slider format.

How I achieve this, help me.

Thanks

Here is a working jsfiddle using the latest version of Owl with what you want/need.

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

With this CSS I am centering the controls and hiding the overflow:

.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;
}

And this JS changes the left margin of the dots, so the active one is always in view. Looks more complicated that it is, I had to handle the exceptions (towards the ends).

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});
});

The principle behind the code is quite simple: I setup the .owl-controls as a small viewing window with overflow: hidden; .

Behind it, I let the .owl-dots have 100% of device screen width. On changing the slide, I change the dots container's position using left , to make sure the active dot is always in the middle, except when we're at the first or last 2 slides.

Try Below Code :-

        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);
                }
            }

        }

Also add these functions in top and change the id name:

 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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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