float: left;
overflow: hidden
transform: translateX(-100vw);
//html
<div style="overflow: hidden">
<div class="slide-container show-1-img">
<div class="slide-box">
<img src="img/money.jpg" />
</div>
<div class="slide-box">
<img src="img/coding.jpg" />
</div>
<div class="slide-box">
<img src="img/health.jpg" />
</div>
<div class="slide-box">
<img src="img/cat.jpg" />
</div>
</div>
</div>
<button class="slide-1-btn">1</button>
<button class="slide-2-btn">2</button>
<button class="slide-3-btn">3</button>
<button class="slide-4-btn">4</button>
//css
.slide-box {
width: 100vw;
float: left; //이미지를 가로로 배치되도록 한다.
}
.slide-box img {
width: 100%;
}
.slide-container {
width: 300vw;
transition: transform 1s;
}
.show-2-img {
transform: translateX(-100vw);
}
.show-3-img {
transform: translateX(-200vw);
}
.show-4-img {
transform: translateX(-300vw);
}
//js
//1번 버튼클릭하면, 1번 이미지로 이동
$(".slide-1-btn").on("click", function () {
$(".slide-container").addClass("show-1-img");
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").removeClass("show-4-img");
});
$(".slide-2-btn").on("click", function () {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-2-img");
});
$(".slide-3-btn").on("click", function () {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-3-img");
});
$(".slide-4-btn").on("click", function () {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").addClass("show-4-img");
});
//오른쪽 버튼클릭하면, 오른쪽 이미지로 이동
$(".slide-left-btn").on("click", function () {
if ($(".slide-container").hasClass("show-4-img")) {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-3-img");
} else if ($(".slide-container").hasClass("show-3-img")) {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-2-img");
} else if ($(".slide-container").hasClass("show-2-img")) {
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-1-img");
}
});
$(".slide-right-btn").on("click", function () {
if ($(".slide-container").hasClass("show-1-img")) {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-2-img");
} else if ($(".slide-container").hasClass("show-2-img")) {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-4-img");
$(".slide-container").addClass("show-3-img");
} else if ($(".slide-container").hasClass("show-3-img")) {
$(".slide-container").removeClass("show-1-img");
$(".slide-container").removeClass("show-2-img");
$(".slide-container").removeClass("show-3-img");
$(".slide-container").addClass("show-4-img");
}
});
let nowPicture = 1;
변수를 만듬if (nowPicture === 1) {
$(".slide-container").css("transform", "translateX(-100vw)");
nowPicture = 2;
}
//js 전체 코드
let nowPicture = 1;
$(".slide-right-btn").on("click", function () {
if (nowPicture === 1) {
$(".slide-container").css("transform", "translateX(-100vw)");
nowPicture = 2;
} else if (nowPicture === 2) {
$(".slide-container").css("transform", "translateX(-200vw)");
nowPicture = 3;
} else if (nowPicture === 3) {
$(".slide-container").css("transform", "translateX(-300vw)");
nowPicture = 4;
}
});
$(".slide-left-btn").on("click", function () {
if (nowPicture === 4) {
$(".slide-container").css("transform", "translateX(-200vw)");
nowPicture = 3;
} else if (nowPicture === 3) {
$(".slide-container").css("transform", "translateX(-100vw)");
nowPicture = 2;
} else if (nowPicture === 2) {
$(".slide-container").css("transform", "translateX(0)");
nowPicture = 1;
}
});
//html
<div style="overflow: hidden; position: relative">
<div class="slide-container">
<div class="slide-box">
<img src="img/money.jpg" />
</div>
<div class="slide-box">
<img src="img/coding.jpg" />
</div>
<div class="slide-box">
<img src="img/health.jpg" />
</div>
<div class="slide-box">
<img src="img/waitress-.jpg" />
</div>
</div>
<button class="slide-left-btn">
<i class="fas fa-long-arrow-alt-left"></i>
</button>
<button class="slide-right-btn">
<i class="fas fa-long-arrow-alt-right"></i>
</button>
</div>
float: left;
설정overflow: hidden;
설정transition: transform 1s;
설정//css
.slide-box {
width: 100vw;
float: left;
}
.slide-box img {
width: 100%;
}
.slide-container {
width: 400vw;
transition: transform 1s;
background-color: tomato;
}
.slide-left-btn {
position: absolute;
left: 0px;
top: 45%;
}
.slide-right-btn {
position: absolute;
right: 0px;
top: 45%;
}
transform: translateX(-100vw);
-100vw
이동하여, 2번째 이미지가 보이도록 했음transform: translateX(-200vw);
설정하면 된다.transition: transform 1s;
을 설정한다.let nowPicture = 1;
변수를 만듬-100vw
이동하면, 두번째 사진이 보인다.-100 = 변수 * -100
-200vw
이동하면, 세번째 사진이 보인다-200 = 변수 * -100
$(".slide-container").css(
"transform",
"translateX(-" + nowPicture + "00vw)"
);
//js 전체코드
let nowPicture = 1;
let 이동;
$(".slide-right-btn").on("click", function () {
if (nowPicture > 0 && nowPicture < 4) {
$(".slide-container").css(
"transform",
"translateX(-" + nowPicture + "00vw)"
);
nowPicture = nowPicture + 1;
}
});
$(".slide-left-btn").on("click", function () {
if (nowPicture > 1 && nowPicture <= 4) {
이동 = (nowPicture - 2) * -100;
$(".slide-container").css("transform", "translateX(" + 이동 + "vw)");
nowPicture = nowPicture - 1;
}
});
position: relative"
와 position: absolute;
을 쓸 수 있다.position: relative"
position: absolute;
top: 50%
세로를 기준으로 중앙에 배치가능<div>
<button></button>
</div>
"ㅁㅁㅁㅁㅁㅁㅁㅁㅇㅇㅇㅇㅇㅇㅇㅇㅇㅇ"
1. 변수를 넣고 싶은 곳을 기준으로 문자열을 쪼갠다.
"ㅁㅁㅁㅁㅁㅁㅁ" "ㅇㅇㅇㅇㅇㅇㅇㅇ"
2. 쪼갠 부분에 ++ 넣는다.
"ㅁㅁㅁㅁㅁㅁㅁ" ++ "ㅇㅇㅇㅇㅇㅇㅇㅇ"
3. ++사이에 변수를 넣는다.
"ㅁㅁㅁㅁㅁㅁㅁ" + i + "ㅇㅇㅇㅇㅇㅇㅇㅇ"