Swiper란 모바일 및 데스크탑에 사용하기 적합한 오프소스 JavaScript 라이브러리
Swiper.js는 여러 개의 CDN을 제공하고 있습니다. 따라서 가장 간편하게 Swiper.js를 사용할 수 있는 방법 중 하나는, CDN을 이용하여 Swiper.js를 로드하는 것입니다.
<!-- CSS -->
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css" /><!-- JS -->
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
Swiper.js를 로컬에 다운로드하여 사용할 수도 있습니다. 이 방법은 Swiper.js의 소스코드를 직접 수정하거나 커스터마이징할 필요가 있을 때 유용합니다.
먼저, Swiper.js 소스코드를 다운로드 받은 후, HTML 파일에서 로드할 수 있도록 경로를 지정해주어야 합니다.
<!-- CSS -->
<link rel="stylesheet" href="path/to/swiper-bundle.min.css" />
<!-- JS -->
<script src="path/to/swiper-bundle.min.js"></script>
위의 코드에서 path/to/
를 Swiper.js 소스코드가 저장된 경로로 변경하여 사용하면 됩니다.
npm을 이용하여 Swiper.js를 설치하는 방법도 있습니다. 이 방법은 npm을 사용하는 프로젝트에서 Swiper.js를 사용할 때 유용합니다.
먼저, npm 명령어를 이용하여 Swiper.js를 설치합니다.
npm install swiper
Swiper.js가 node_modules 폴더에 설치되면, 이후 방법은 로컬 설치 방법과 동일합니다.
const mySwiper = new Swiper('.swiper-container', {
direction: 'horizontal'
});
const mySwiper = new Swiper('.swiper-container', {
loop: true
});
const mySwiper = new Swiper('.swiper-container', {
autoplay: {
delay: 3000
}
});
const mySwiper = new Swiper('.swiper-container', {
speed: 500
});
const mySwiper = new Swiper('.swiper-container', {
slidesPerView: 2
});
const mySwiper = new Swiper('.swiper-container', {
pagination: {
el: '.swiper-pagination',
type: 'bullets'
}
});
const mySwiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
const mySwiper = new Swiper('.swiper-container', {
scrollbar: {
el: '.swiper-scrollbar',
hide: true
}
});
const mySwiper = new Swiper('.swiper-container', {
effect: 'fade'
});
let swiper = new Swiper('#id', {
slidesPerView : 보여질 개수,
spaceBetween : 아이템 사이 간격,
slidesPerGroup : 한번에 슬라이딩될 개수,
centeredSlides: true, //센터모드
threshold:100, //마우스 스와이프 민감도
autoplay:{
delay: 2500, // 시간 설정
disableOnInteraction: false, // false-스와이프 후 자동 재생
loop: 무한반복 할지말지,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
pagination : { // 페이저 버튼 사용자 설정
el : '.pagination', // 페이저 버튼을 담을 태그 설정
clickable : true, // 버튼 클릭 여부
type : 'bullets', // 버튼 모양 결정 "bullets", "fraction"
renderBullet : function (index, className) { // className이 기본값이 들어가게 필수 설정
return '<a href="#" class="' + className + '">' + (index + 1) + '</a>'
},
renderFraction: function (currentClass, totalClass) { // type이 fraction일 때 사용
return '<span class="' + currentClass + '"></span>' + '<span class="' + totalClass + '"></span>';
}
},
scrollbar:{
el : '선택자',
draggable: 스크롤바 이동으로 스와이프가 가능하게 할지,
dragSize: 스크롤바 크기,
},
a11y: { // 웹접근성
enabled: true,
prevSlideMessage: '이전 슬라이드',
nextSlideMessage: '다음 슬라이드',
slideLabelMessage: '총 {{slidesLength}}장의 슬라이드 중 {{index}}번 슬라이드 입니다.',
},
breakpoints:{
280: {
//280px 이하의 크기에서 옵션 값
},
768 : {
//768px 이하의 크기에서 옵션 값
},
1024 : {
//1024px 이하의 크기에서 옵션 값
}
}
})
breakpoints : { // 반응형 설정이 가능 width값으로 조정
768 : {
slidesPerView : 1,
},
},
//5.3.0부터 "비율"(너비 / 높이)로 설정이 가능해졌습니다.
var swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 10,
breakpoints: {
'@0.75': {
slidesPerView: 2,
spaceBetween: 20,
},
'@1.00': {
slidesPerView: 3,
spaceBetween: 40,
},
'@1.50': {
slidesPerView: 4,
spaceBetween: 50,
},
}
});
swiper이름.메소드() 형식으로 사용할 수 있다.
// ex) mySlider.autoplay.start()
.slideTo(index, speed, runCallbacks) // 해당 슬라이드로 이동
.slidePrev(index, speed, runCallbacks) // 이전 슬라이드로 이동
.slideNext(index, speed, runCallbacks) // 다음 슬라이드로 이동
.autoplay.start(); // 자동 재생 시작
.autoplay.stop(); // 자동 재생 정지
.destroy() // 슬라이드 제거
//방법 1
var swiper = new Swiper('.m_swiper', {
loop: true,
loopFillGroupWithBlank: true,
spaceBetween: 8, // 슬라이드 간격
slidesPerView: 1.08, // 한번에 보이는 슬라이드 갯수
centeredSlides: true, // 양쪽 미리보기
pagination: {
el: '.swiper-pagination',
type: 'fraction',
},
});
//방법 2
slidesPerView: ‘auto’,
.swiper slide {width: 93%} 로 가로값 강제하기
.swiper-wrapper {
transition-timing-function: linear;
}
var swiper = new Swiper(".swiper", {
loop: true,
centeredSlides: true,
speed: 7000,
autoplay: {
delay: 0,
disableOnInteraction: false
},
slidesPerView: 'auto',
});