์ฌ๋ผ์ด๋ ์ดํํธ - ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋(๋ฒํผ, dot๋ฉ๋ด, ๋ฌดํ์ผ๋ก ์์ง์ด๊ธฐ)
์ด๋ฒ ์๊ฐ์๋ ์ด๋ฏธ์ง๊ฐ ๋๊ธฐ์ง ์๊ณ ๊ณ์ ์ด์ด์ ธ์ ๋์ค๋ ๋ฌดํ ์ฌ๋ผ์ด๋์ ๋ท๋ฉ๋ด ๋ฒํผ์ผ๋ก ์์ง์ด๋ ์ฌ๋ผ์ด๋๋ฅผ ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค! ๐ฅน
#1. HTML / CSS ์ค์ ํ๊ธฐ
! HTML TIP !
๊ฐ ์ด๋ฏธ์ง๋ง๋ค css์ค์ ์ ์ฃผ๊ธฐ ์ํด ๋ฐ๋ก ๋ถ๋ชจ๋ฅผ 2๋ฒ ๊ฐ์ธ์ ๋ฌถ์ด์ค๋๋ค.
๋ํ ๋ฒํผ ์๋ ๋ท๋ฉ๋ด ๊ตฌ์กฐ๋ฅผ ์ถ๊ฐํด์ฃผ์์ต๋๋ค. ๋ท ๋ฉ๋ด๋ ์คํฌ๋ฆฝํธ๋ก ๊ตฌํํ ๊ฒ์ด๋ฏ๋ก ์ฃผ์์ฒ๋ฆฌ๋ฅผ ํด์ค๋๋ค.
โ๏ธ HTML ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ
<main id="main">
<section id="sliderType04">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider__inner">
<div class="slider s1" role="group" aria-label="1/5">
<span>์ด๋ฏธ์ง1</span>
<img src="../assets/img/Mountian_bg01.jpg" alt="์ด๋ฏธ์ง6">
</div>
<div class="slider s2" role="group" aria-label="2/5">
<span>์ด๋ฏธ์ง2</span>
<img src="../assets/img/Mountian_bg02.jpg" alt="์ด๋ฏธ์ง7">
</div>
<div class="slider s3" role="group" aria-label="3/5">
<span>์ด๋ฏธ์ง3</span>
<img src="../assets/img/Mountian_bg03.jpg" alt="์ด๋ฏธ์ง8">
</div>
<div class="slider s4" role="group" aria-label="4/5">
<span>์ด๋ฏธ์ง4</span>
<img src="../assets/img/Mountian_bg04.jpg" alt="์ด๋ฏธ์ง9">
</div>
<div class="slider s5" role="group" aria-label="5/5">
<span>์ด๋ฏธ์ง5</span>
<img src="../assets/img/Mountian_bg05.jpg" alt="์ด๋ฏธ์ง10">
</div>
</div>
</div>
<div class="slider__btn">
<button href="#" class="prev" role="button" aria-label="์ผ์ชฝ ์ด๋ฏธ์ง">prev</button>
<button href="#" class="next" role="button" aria-label="์ค๋ฅธ์ชฝ ์ด๋ฏธ์ง">next</button>
</div>
<div class="slider__dot">
<!-- <a href="#" class="dot active">์ด๋ฏธ์ง1</a>
<a href="#" class="dot">์ด๋ฏธ์ง2</a>
<a href="#" class="dot">์ด๋ฏธ์ง3</a>
<a href="#" class="dot">์ด๋ฏธ์ง4</a>
<a href="#" class="dot">์ด๋ฏธ์ง5</a> -->
</div>
</div>
</section>
</main>
<!-- //main -->
! CSS TIP !
1. ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ก๋ก ์ฐ๊ฒฐํ๊ธฐ ์ํด ํฌ์ง์
์ relative๋ก ์ค์ ํ๊ณ ์์ญ์์ ๋ฒ์ด๋๋ ์ด๋ฏธ์ง๋ฅผ ์๋ณด์ด๊ฒ ์ฒ๋ฆฌํ๊ธฐ ์ํด overflow: hidden์ ์ค์ ํด์ค๋๋ค.
2. ์ด๋ฏธ์ง ๊ฐ๋ก ๊ฐ์ ์ด๋ฏธ์ง๊ฐ ์๋ฆฌ์ง ์๋ ์ ์์ ์ต๋๋ก ์ค์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
3. pointer-events: none์ ํตํด ๋ฒํผ์ด ์ฐ์์ผ๋ก ๋๋ฆฌ์ง์๊ฒ ํฉ๋๋ค.
โ๏ธ CSS ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ
/* slider */
.slider__wrap {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.slider__img { /* ์ด๋ฏธ์ง๊ฐ ๋ณด์ด๋ ์์ญ */
position: relative;
width: 800px;
height: 450px;
* overflow: hidden;
}
.slider__inner {/* ์ ์ฒด ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ธ๊ณ ์๋ ๋ถ๋ชจ : ์์ง์ด๋ ๋ถ๋ถ ์์ฑ์ฃผ๊ธฐ */
display: flex;
flex-wrap: wrap;
width: 10000px; /* ์คํฌ๋ฆฝํธ๋ก ์ฒ๋ฆฌํ ๊ฒ์ด๋ฏ๋ก ์๊นจ์ง๊ฒ ๋ width๊ฐ ์ฃผ๋ฉด ๋จ */
height: 450px;
position: relative;
left: -800px; /* ์ฌ๋ผ์ด๋ ์ด๊ธฐ๊ฐ */
}
/* ๋ง์ง๋ง์์ ์ฒ์์ผ๋ก ์๊ฐ์ด๋ */
.slider__inner.transition {
transition: all 0.3s;
}
.slider { /* ๊ฐ๋ณ์ ์ธ ์ด๋ฏธ์ง (๊ฐ๊ฐ์ ์ด๋ฏธ์ง ์ค์ ) */
position: relative;
width: 800px;
height: 450px;
}
.slider span {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 5px 10px;
}
@media(max-width : 800px){
.slider__img {
width: 400px;
height: 250px;
}
.slider {
width: 400px;
height: 250px;
}
}
/* ์ฌ๋ผ์ด๋ ๋ฒํผ ์ฐ์์ผ๋ก ๋๋ฅผ ์ ์๊ฒ ํ๊ธฐ 01 */
.slider__btn.disable button{
pointer-events: none;
}
.slider__btn button {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background: rgba(0,0,0,0.4);
text-align: center;
line-height: 50px;
transition: all 0.2s;
display: block;
color: #fff;
pointer-events: auto;
border: 0;
cursor: pointer;
}
.slider__btn button:hover {
border-radius: 50px;
background: rgba(38, 52, 103, 0.4);
}
.slider__btn button.prev {
left: 0;
}
.slider__btn button.next {
right: 0;
}
.slider__dot {
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 20px;
}
.slider__dot .dot {
width: 20px;
height: 20px;
background: rgba(0,0,0,0.4);
display: inline-block;
border-radius: 50%;
text-indent: -9999px;
transition: all 0.3s;
margin: 3px;
}
.slider__dot .dot.active {
background: rgba(255,255,255,0.9);
}
#2. JAVASCRIPT : ์คํฌ๋ฆฝํธ ์ง๊ธฐ
cloneFirst = sliderFirst.cloneNode(true) / cloneLast = sliderLast.cloneNode(true)๋ฅผ ํตํด ์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ง์ง๋ง ์ด๋ฏธ์ง๋ฅผ ๋ณต์ฌํด์ค๋๋ค.
๋งค๊ฐ๋ณ์์ ํจ์๋ฅผ ์ด์ฉํ์ฌ ์คํฌ๋ฆฝํธ๋ฅผ ๋ ํจ๊ณผ์ ์ผ๋ก ์์ฑํด์ค๋๋ค.
๊ทธ ์ธ ์ธ๋ถ์ ์ธ ๋ถ๋ถ์ ์๋์ ์ฃผ์๊ณผ ์ฝ๋๋ฅผ ํตํด ์ดํด๋ณด๊ฒ ์ต๋๋ค!
const sliderWrap = document.querySelector(".slider__wrap"); //์ ์ฒด ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋
const sliderImg = document.querySelector(".slider__img"); //๋ณด์ฌ์ง๋ ์์ญ
const sliderInner = document.querySelector(".slider__inner"); //์์ง์ด๋ ์์ญ
const slider = document.querySelectorAll(".slider"); //์ด๋ฏธ์ง
const sliderDot = document.querySelector(".slider__dot"); //๋ท ๋ฉ๋ด
const sliderBtn = document.querySelector(".slider__btn"); //๋ฒํผ
const sliderBtnPrev = document.querySelector(".slider__btn .prev"); //์ผ์ชฝ ๋ฒํผ
const sliderBtnNext = document.querySelector(".slider__btn .next"); //์ค๋ฅธ์ชฝ ๋ฒํผ
let currentIndex = 0, //ํ์ฌ ์ด๋ฏธ์ง
sliderLength = slider.length, //์ฌ๋ผ์ด๋ ์ด ๊ธธ์ด
sliderWidth = slider[0].offsetWidth, //์ฌ๋ผ์ด๋ ๊ฐ๋ก ๊ฐ
sliderFirst = slider[0], //์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง
sliderLast = slider[sliderLength -1], //๋ง์ง๋ง ์ด๋ฏธ์ง
cloneFirst = sliderFirst.cloneNode(true), //์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง ๋ณต์ฌ
cloneLast = sliderLast.cloneNode(true); //๋ง์ง๋ง ์ด๋ฏธ์ง ๋ณต์ฌ
sliderInner.appendChild(cloneFirst);
sliderInner.insertBefore(cloneLast, sliderFirst);
function init(){
let dotIndex = "";
//์ด๋ฏธ์ง ๊ฐฏ์๋งํผ ๋ท ๋ฉ๋ด ์๊ธฐ๋๋ก
for(let i=0; i<sliderLength; i++){
dotIndex += "<a href='#' class='dot'>์ด๋ฏธ์ง1</a>"
sliderDot.innerHTML = dotIndex;
sliderDot.firstChild.classList.add("active");
}
}
init();
//๋งค๊ฐ๋ณ์ ์ฌ์ฉ - 5๋ฒ ์ฌ๋ผ์ดํธ ์ดํํธ ๋ณด๋ค ์ธ๋ถํ ์ํค๊ธฐ
function gotoSlider(direction){
sliderInner.classList.add("transition");
// ์ฌ๋ผ์ด๋ ๋ฒํผ ์ฐ์์ผ๋ก ๋๋ฅผ ์ ์๊ฒ ํ๊ธฐ 02
sliderBtn.classList.add("disable");
posInital = sliderInner.offsetLeft;
// console.log(posInital);
//direction์ด๋ผ๋ ๋งค๊ฐ๋ณ์์ -1์ด ๋ค์ด์จ๋ค๋ฉด ์ผ์ชฝ์ผ๋ก ์ด๋(์ด์ ) ๋ํ 1์ด๋ผ๋ฉด ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋(๋ค์)
if(direction == -1){
// sliderInner.style.left = "800px"; //์ผ์ชฝ์ผ๋ก 800๋งํผ ์์ง์ด๊ธฐ
//๋ถ๋ชจ๋ฐ์ค offsetLeft๊ฐ์ ๊ตฌํ์ฌ ๋ํ๋ ์ด์ ๋? sliderWidth๋ ์ฌ๋ผ์ด๋ ํ๋๋ง์ ๊ฐ๋ก๊ฐ์ด๊ธฐ ๋๋ฌธ์ ์ด๋์ ๋์ด๋๊ฑฐ๋ ์ค์ด๋๋ ๊ฐ์ ๊ตฌํ ์ ์๋ค ๋๋ฌธ์ ๊ทธ ์ฌ๋ผ์ด๋๋ฅผ ๊ฐ์ธ๋ ๋ถ๋ชจ๋ฐ์ค์ ๊ฐ๋ก๊ฐ์ ๊ตฌํ์ฌ ๋์ด๋๋ ๊ฐ ๋งํผ ๊ธธ์ด๋ฅผ ๋ํ์ฌ ์ถ๊ฐ์ํจ ๊ฒ ์ด๋ค.
sliderInner.style.left = (posInital + sliderWidth) + "px"; //์ผ์ชฝ์ผ๋ก ์ฌ๋ผ์ด๋ ๊ฐ๋ก๊ฐ๋งํผ ์์ง์ด๊ธฐ. ์ฌ๋ผ์ด๋์ ๋ถ๋ชจ๋ฐ์ค์ ์ผ์ชฝ๊ฐ์ ๋ํ ์ด์ ๋?
currentIndex --;
}else if(direction == 1){
// sliderInner.style.left = "-800px";
sliderInner.style.left = (posInital - sliderWidth) + "px";
currentIndex ++;
}
}
// ์๊ฐ์ด๋ ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋ (๋ฌดํ ์ฌ๋ผ์ด๋)
function checkIndex(){
//์ฌ๋ผ์ด๋๊ฐ ๋๋๋ฉด transition ํด๋์ค๋ฅผ ๋ค์ ์ง์์ transitionํจ๊ณผ๊ฐ ๋์ค์ง ์๋๋ก ํ๊ณ ์ด๋ฏธ์ง ์๊ฐ์ด๋ ์ํค๊ธฐ
sliderInner.classList.remove("transition");
// console.log(currentIndex);
if(currentIndex == sliderLength){
sliderInner.style.left = -(1 * sliderWidth) + "px";
currentIndex = 0;
}
if(currentIndex == -1){
sliderInner.style.left = -(sliderLength * sliderWidth) + "px";
currentIndex = sliderLength -1;
}
}
//์คํ๋ฌธ
sliderBtnPrev.addEventListener("click", () => {
gotoSlider(-1); //๋งค๊ฐ๋ณ์ direction์ ๊ฐ ์ฃผ๊ธฐ
setTimeout(() => {
sliderBtn.classList.remove("disable");
},300);
});
sliderBtnNext.addEventListener("click", () => {
gotoSlider(1); //๋งค๊ฐ๋ณ์ direction์ ๊ฐ ์ฃผ๊ธฐ
setTimeout(() => {
sliderBtn.classList.remove("disable");
},300);
});
// ํธ๋ ์ง์
์ด๋ฒคํธ๊ฐ ๋๋ฌ์ ๋ ์ผ์ด๋๋ ์ด๋ฒคํธ
sliderInner.addEventListener("transitionend", checkIndex);
๋๊ธ