<div class="slide-box">
<div class="slides">
<a href="#" class="slide">
<img src="../img/1.jpg" alt="" />
</a>
<a href="#" class="slide">
<img src="../img/2.jpg" alt="" />
</a>
<a href="#" class="slide">
<img src="../img/3.jpg" alt="" />
</a>
</div>
<div class="slide-nav">
<a href="#" class="prev"><i class="bi bi-chevron-left"></i></a>
<a href="#" class="next"><i class="bi bi-chevron-right"></i></a>
</div>
<div class="slide-dot"></div>
</div>
var sCount = 3;
var sCurrent = 0;
var imgw = $(".slide").width();
var timer;
//하얀점넣고 클릭시
for (let i = 0; i < sCount; i++) {
var dots = `<a href="#">${i}</a>`;
$(".slide-dot").append(dots);
$(".slide-dot a")
.eq(i)
.on("click", function (e, i) {
e.preventDefault;
if ($(e.currentTarget).hasClass("active")) {
return;
}
sNext = $(e.currentTarget).index();
gotoSlide(sNext);
$(".slide-dot a").removeClass("active");
$(e.currentTarget).addClass("active");
});
}
function gotoSlide(sNext) {
$(".slides .slide").fadeOut();
$(".slides .slide").eq(sNext).fadeIn();
$(".slide-dot a").removeClass("active");
$(".slide-dot a").eq(sNext).addClass("active");
}
//이전 다음 버튼
$(".slide-nav .next").on("click", function () {
sNext++;
if (sNext == 3) {
sNext = 0;
}
gotoSlide(sNext);
sCurrent = sNext;
});
$(".slide-nav .prev").on("click", function () {
sNext--;
if (sNext == -1) {
sNext = 2;
}
gotoSlide(sNext);
sCurrent = sNext;
});
//타이머;
function sTimer() {
timer = setInterval(function () {
sNext++;
if (sNext == 3) {
sNext = 0;
}
gotoSlide(sNext);
sCurrent = sNext;
}, 4000);
}
function stopTimer() {
clearInterval(timer);
}
$(".slide, .slide-nav").on("mouseenter", function () {
stopTimer();
});
$(".slide, .slide-nav").on("mouseleave", function () {
sTimer();
});
// 초기 세팅
$(".slide-dot a").eq(0).trigger("click");
sTimer();
'Js' 카테고리의 다른 글
addClass, 아코디언, tab, attr, each, hasClass (0) | 2024.07.01 |
---|---|
addEventListener (1) | 2024.07.01 |
scroll, resize, offset().top (0) | 2024.06.30 |
다크모드 로컬 저장 (0) | 2024.06.30 |
masonry 메이슨리 레이아웃 (0) | 2024.06.30 |
rect() 를 이용한 마스크 효과 (0) | 2024.06.30 |
플러그인 없이 패럴렉스 효과 (0) | 2024.06.29 |
offset().top 메뉴 스크롤하기 (0) | 2024.06.29 |