Swiper 슬라이더를 사용하면 웹페이지에 쉽게 슬라이드 기능을 삽입할 수 있습니다.
심지어는 반응 형 웹을 개발할 때도 옵션 만으로 쉽게 반응 형 기능을 구현할 수 있습니다.
autoHeight : true;
위와 같이 옵션을 주면 이미지의 넓이에 맞춰서 이미지의 높이가 자동 조절됩니다.
하지만 이미지의 크기가 그리 크지 않을 때에는 화면을 줄임에 따라 이미지의 높이가 자동
조절 되면서 이미지가 너무 작아져 버리는 문제가 생깁니다.
그래서 이미지가 줄어들 때 이미지의 넓이에 맞춰서 높이를 강제 지정해 주어야 하는 것이
필요할 때도 생깁니다.
<body>
<section class="main swiper mySwiper">
<div class="wrapper swiper-wrapper">
<div class="slide swiper-slide">
<img src="images/img01.jpg" alt="" class="image" />
</div>
<div class="slide swiper-slide">
<img src="images/img02.jpg" alt="" class="image" />
</div>
<div class="slide swiper-slide">
<img src="images/img03.jpg" alt="" class="image" />
</div>
</div>
<div class="swiper-button-next nav-btn"></div>
<div class="swiper-button-prev nav-btn"></div>
<div class="swiper-pagination"></div>
</section>
<!-- Swiper JS -->
<script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>
<!-- Initialize Swiper -->
<script>
var swiper = new Swiper(".mySwiper", {
slidesPerView: 1,
autoplay: { //자동슬라이드 (false-비활성화)
delay: 2500, // 시간 설정
disableOnInteraction: false, // false-스와이프 후 자동 재생
},
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
</script>
</body>
위의 코드는 Swiper 슬라이더에서 제공해 주는 기본 코드 중에 하나 입니다.
브라우저의 화면 크기에 따라 높이도 자동으로 조절되는 듯이 보이지만 마우스를 이용
해서 브라우저의 넓이를 서서히 줄이면 높이가 변하지 않고 그대로 유지 됩니다.
마우스를 이용해서 브라우저의 크기를 변경할 때는 아래와 CSS 코드를 만들어 주시면
됩니다.
<style>
@media screen and (max-width: 1024px) {
.swiper{
height: 650px !important;
}
}
@media screen and (max-width: 768px) {
.swiper{
height: 450px !important;
}
}
</style>
Swiper 슬라이더를 아래와 같이 CDN으로 사용하므로 ! important 선언을 해서 height 속성의 우선 순위를
높여 줍니다.
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css"
/>
댓글
댓글 쓰기