<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide slide1">
<p>slide1</p>
</div>
<div class="swiper-slide slide2">
<p>slide2</p>
</div>
<div class="swiper-slide slide3">
<p>slide3</p>
</div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-pagination"></div>
</div>
.swiper {
width: 100%;
height: 200px;
}
.swiper-slide {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-weight: bold;
}
.slide1 {
background-color: #88acbd;
}
.slide2 {
background-color: #99cb1f;
}
.slide3 {
background-color: #e43a47;
}
.swiper:not(.swiper-initialized) .swiper-button-prev,
.swiper:not(.swiper-initialized) .swiper-button-next {
display: none;
}
.swiper:not(.swiper-initialized) .swiper-wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
const mediaQueryList = window.matchMedia("(max-width: 599px)");
let swiper = null;
const initSwiper = () => {
if (mediaQueryList.matches) {
swiper = new Swiper(".swiper", {
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev"
},
pagination: {
el: ".swiper-pagination",
type: "bullets",
clickable: "clickable"
},
loop: true,
autoplay: true
});
} else {
if (swiper) {
swiper.destroy(false, true);
}
}
};
mediaQueryList.addEventListener("change", initSwiper);
initSwiper();