<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Document</title>
<script>
$(function() {
});
</script>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
section {
overflow: hidden;
width: 600px;
height: 500px;
margin: 100px auto;
}
.sliderList {
width: 300%;
}
.sliderList li {
float: left;
width: 33.3333%;
height: 400px;
}
.btnList { clear: both; width: 90px; margin: 0 auto; padding-top: 20px;}
.btnList span { display: block; float: left; width: 20px; height: 20px; margin: 0 5px; background: #666;
border-radius: 20px; text-indent: -9999px; cursor: pointer;}
.btnList .on { background-color: red;}
</style>
</head>
<body>
<section>
<div>
<ul class="sliderList">
<li><img src="https://via.placeholder.com/600x400?text=1" /></li>
<li><img src="https://via.placeholder.com/600x400?text=2" /></li>
<li><img src="https://via.placeholder.com/600x400?text=3" /></li>
</ul>
<div class="btnList">
<span class="btn1 on">첫번쨰</span>
<span class="btn2">두번째</span>
<span class="btn3">세번째</span>
</div>
</div>
</section>
<script>
const imageWidth = 600;
const move = function () {
let idx = $('.btnList span.on').index();
$(".btnList span").removeClass('on').eq((idx + 1) % 3).addClass('on');
console.log($('.sliderList').css('margin-left'));
if (parseInt($('.sliderList').css('margin-left')) < -(imageWidth)) {
$(".sliderList").stop().animate({marginLeft:0})
}
else {
if (parseInt($(".sliderList").css('margin-left')) === 0) {
}
else if (parseInt($(".sliderList").css('margin-left')) === -(imageWidth)) {
}
$('.sliderList').stop().animate({marginLeft: "-=" + imageWidth + "px"});
};
}
let timer = setInterval(move, 3 * 1000);
$('.btnList span').on('click', function() {
clearInterval(timer);
timer = setInterval(move, 3 * 1000);
let num = $(this).index();
console.log(num);
$(".sliderList").stop().animate({marginLeft: -(imageWidth) * num})
$('.btnList span').eq(num).addClass('on').siblings().removeClass('on');
})
</script>
</body>
</html>