Страницы

Поиск по вопросам

суббота, 21 декабря 2019 г.

OwlCarousel2. Центрирование thumbnails по клику при loop: true

#javascript #jquery #вёрстка #owl_carousel


Есть две owl-карусельки: превьюшная и основная. Они синхронизированы. Обе карусели
имеют loop: true. Не получается заставить превьюшный слайдер центрировать owl-item-s
по клику на них. Что я делаю не так?

Песочница здесь. Код, отвечающий за желаемое поведение, помечу комментарием centered
items (коммент будет в двух местах).



$(document).ready(function() {

    var sync1 = $("#sync1");
    var sync2 = $("#sync2");
    var slidesPerPage = 4;
    var syncedSecondary = true;

    sync1.owlCarousel({
        items: 1,
        slideSpeed: 2000,
        nav: true,
        autoplay: false, 
        dots: true,
        loop: true,
        responsiveRefreshRate: 200,
        navText: ['', ''],
    }).on('changed.owl.carousel', syncPosition);

  sync2.on('initialized.owl.carousel', function() {
    sync2.find(".owl-item.center").eq(0).addClass("current");
  })
  
  /* centered items */
  sync2.find('.owl-item').each(function(index) {
    var item = $(this).attr('data-position', index);
  })
  
  sync2.owlCarousel({
    items: slidesPerPage,
    dots: false,
    nav: false,
    loop: true,
    center: true,
    smartSpeed: 200,
    slideSpeed: 1000,
    slideBy: slidesPerPage,
    responsiveRefreshRate: 100
  }).on('click', '.owl-item', function(e) {
        var carouselSync1 = $('#sync1').data('owl.carousel');
        e.preventDefault();
    
        var current = $(this).index();
        carouselSync1.to(carouselSync1.relative(current));
        
        /* centered items */
        sync2.trigger('to.owl-carousel', $(this).data('position'));
      });

    function syncPosition(el) {
       
        var current = el.item.index;
      
        sync2.find(".owl-item").removeClass("current").eq(current).addClass("current");
        var onscreen = sync2.find('.owl-item.active').length - 1;
        var start = sync2.find('.owl-item.active').first().index();
        var end = sync2.find('.owl-item.active').last().index();
      
        if (current > end) {
          sync2.data('owl.carousel').to(current, 100, true);
        }
        if (current < start) {
          sync2.data('owl.carousel').to(current - onscreen, 100, true);
        }
    }

    function syncPosition2(el) {
      if (syncedSecondary) {
        var number = el.item.index;
        sync1.data('owl.carousel').to(number, 100, true);
      }
    }
});
#sync1 .item {
  background: #0c83e7;
  padding: 80px 0px;
  margin: 5px;
  color: #fff;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  text-align: center;
}

#sync2 .item {
  background: #c9c9c9;
  padding: 10px 0px;
  margin: 5px;
  color: #fff;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  text-align: center;
  cursor: pointer;
}
#sync2 .item h1 {
  font-size: 18px;
}
#sync2 .current .item {
  background: #0c83e7;
}

.owl-theme .owl-nav [class*="owl-"] {
  transition: all 0.3s ease;
}
.owl-theme .owl-nav [class*="owl-"].disabled:hover {
  background-color: #d6d6d6;
}

#sync1.owl-theme {
  position: relative;
}
#sync1.owl-theme .owl-next,
#sync1.owl-theme .owl-prev {
  width: 22px;
  height: 40px;
  margin-top: -20px;
  position: absolute;
  top: 50%;
}
#sync1.owl-theme .owl-prev {
  left: 10px;
}
#sync1.owl-theme .owl-next {
  right: 10px;
}















    


Ответы

Ответ 1



$(document).ready(function() { const sync1 = $("#sync1"); const sync2 = $("#sync2"); let slidesPerPage = 4; let syncedSecondary = true; let call_first = true; let correct = 1; sync1.owlCarousel({ items: 1, slideSpeed: 2000, nav: true, autoplay: false, dots: true, loop: true, responsiveRefreshRate: 200, navText: [ ` `, ` ` ], afterAction : syncPosition, }).on('changed.owl.carousel', syncPosition); sync2.owlCarousel({ items: slidesPerPage, dots: false, nav: false, loop: true, center: true, smartSpeed: 200, slideSpeed: 1000, slideBy: slidesPerPage, onInitialized: carousel2Initialized, responsiveRefreshRate: 100, }).on('changed.owl.carousel', el =>{ sync1.trigger('to.owl.carousel', [el.item.index + 4, 200, true]); }); $('#sync2').on('click', '.owl-item', function(e) { const carousel = $('.owl-carousel').data('owl.carousel'); e.preventDefault(); if (call_first) { call_first = false; carousel.to(carousel.relative($(this).index())); sync1.trigger('to.owl.carousel', [carousel.relative($(this).index()), 200, true]); } else { carousel.to(carousel.relative($(this).index())); } }); $('.owl-prev').on('click', () => { if (call_first) { correct = -1; } }); function syncPosition(el) { if (call_first) { setTimeout(() => { call_first = false; sync2.find(".owl-item").removeClass("current").removeClass('active'); sync2.trigger("to.owl.carousel", [correct, 300, true]); sync2.find(".owl-item.center").addClass("current"); }, 0 ); } else { sync2.find(".owl-item").removeClass("current").removeClass('active'); sync2.trigger("to.owl.carousel", [el.item.index - 4, 300, true] ); } sync2.find(".owl-item.center").addClass("current"); } function carousel2Initialized () { $('#sync2 .owl-item.center').addClass('current'); } }); #sync1 .item { background: #0c83e7; padding: 80px 0px; margin: 5px; color: #fff; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; text-align: center; } #sync2 .item { background: #c9c9c9; padding: 10px 0px; margin: 5px; color: #fff; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; text-align: center; cursor: pointer; } #sync2 .item h1 { font-size: 18px; } #sync2 .current .item { background: #0c83e7; } .owl-theme .owl-nav [class*="owl-"] { transition: all 0.3s ease; } .owl-theme .owl-nav [class*="owl-"].disabled:hover { background-color: #d6d6d6; } #sync1.owl-theme { position: relative; } #sync1.owl-theme .owl-next, #sync1.owl-theme .owl-prev { width: 22px; height: 40px; margin-top: -20px; position: absolute; top: 50%; } #sync1.owl-theme .owl-prev { left: 10px; } #sync1.owl-theme .owl-next { right: 10px; } UPD получить индекс по клику.

Комментариев нет:

Отправить комментарий