<div class="swiper-object">
<div class="js-sample-slider swiper-container" data-swiper-id="1">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>
<div class="swiper-navigation" data-swiper-id="1">
<button class="swiper-button-prev">Prev</button>
<button class="swiper-button-next">Next</button>
</div>
</div>
<div class="swiper-object">
<div class="js-sample-slider swiper-container" data-swiper-id="2">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>
<div class="swiper-navigation" data-swiper-id="2">
<button class="swiper-button-prev">Prev</button>
<button class="swiper-button-next">Next</button>
</div>
</div>
//Swiper
$('.swiper-object .js-sample-slider').each(function(index, value) {
const swiperId = $(value).data('swiper-id'); // Получаем ID карусели из data-атрибута
const swiperNav = $('.swiper-navigation[data-swiper-id="' + swiperId + '"]'); // Находим контейнер навигации по ID карусели
const swiper = new Swiper(value, {
slidesPerView: 6,
spaceBetween: 16,
lazy: true, //lazy load
loop: true, //loop
pagination: { //pagination(dots)
el: '.swiper-pagination',
clickable: true,
},
navigation: { //navigation(arrows)
nextEl: swiperNav.find('.swiper-button-next')[0],
prevEl: swiperNav.find('.swiper-button-prev')[0],
},
breakpoints: {
640: {
slidesPerView: 2,
spaceBetween: 20,
},
768: {
slidesPerView: 4,
spaceBetween: 40,
},
1024: {
slidesPerView: 6,
spaceBetween: 16,
},
},
});
});
// Назначение событий на кнопки навигации
$('.swiper-navigation .swiper-button-prev').on('click', function() {
const swiperId = $(this).closest('.swiper-navigation').data('swiper-id');
const swiper = $('.js-sample-slider[data-swiper-id="' + swiperId + '"]')[0].swiper;
swiper.slidePrev();
});
$('.swiper-navigation .swiper-button-next').on('click', function() {
const swiperId = $(this).closest('.swiper-navigation').data('swiper-id');
const swiper = $('.js-sample-slider[data-swiper-id="' + swiperId + '"]')[0].swiper;
swiper.slideNext();
});
slidesPerView: 'auto',только потом не забыть указать в css свою ширину