์ฌ๋ผ์ด๋ ์ดํํธ - ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋(๋ฒํผ, dot๋ฉ๋ด)
์ด๋ฒ ์๊ฐ์๋ ์๋์ผ๋ก ๋์ด๊ฐ์ง ์๊ณ ๋ฒํผ๊ณผ ๋ท ๋ฉ๋ด๋ฅผ ํด๋ฆญํ์ ๋ ํด๋น ์์๋ก ์ด๋ฏธ์ง๊ฐ ๋์ด๊ฐ๋๋ก ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค! ๐ฅน
#1. HTML / CSS ์ค์ ํ๊ธฐ
! HTML TIP !
๊ฐ ์ด๋ฏธ์ง๋ง๋ค css์ค์ ์ ์ฃผ๊ธฐ ์ํด ๋ฐ๋ก ๋ถ๋ชจ๋ฅผ 2๋ฒ ๊ฐ์ธ์ ๋ฌถ์ด์ค๋๋ค.
๋ํ ๋ฒํผ ์๋ ๋ท๋ฉ๋ด ๊ตฌ์กฐ๋ฅผ ์ถ๊ฐํด์ฃผ์์ต๋๋ค. ๋ท ๋ฉ๋ด๋ ์คํฌ๋ฆฝํธ๋ก ๊ตฌํํ ๊ฒ์ด๋ฏ๋ก ์ฃผ์์ฒ๋ฆฌ๋ฅผ ํด์ค๋๋ค.
โ๏ธ HTML ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ
<section id="sliderType05">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider__inner">
<div class="slider" role="group" aria-label="1/5">
<img src="../assets/img/Mountian_bg01.jpg" alt="์ด๋ฏธ์ง6">
</div>
<div class="slider" role="group" aria-label="2/5">
<img src="../assets/img/Mountian_bg02.jpg" alt="์ด๋ฏธ์ง7">
</div>
<div class="slider" role="group" aria-label="3/5">
<img src="../assets/img/Mountian_bg03.jpg" alt="์ด๋ฏธ์ง8">
</div>
<div class="slider" role="group" aria-label="4/5">
<img src="../assets/img/Mountian_bg04.jpg" alt="์ด๋ฏธ์ง9">
</div>
<div class="slider" role="group" aria-label="5/5">
<img src="../assets/img/Mountian_bg05.jpg" alt="์ด๋ฏธ์ง10">
</div>
</div>
</div>
<div class="slider__btn">
<a href="#" class="prev" role="button" aria-label="์ผ์ชฝ ์ด๋ฏธ์ง">prev</a>
<a href="#" class="next" role="button" aria-label="์ค๋ฅธ์ชฝ ์ด๋ฏธ์ง">next</a>
</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>
! CSS TIP !
1. ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ก๋ก ์ฐ๊ฒฐํ๊ธฐ ์ํด ํฌ์ง์
์ relative๋ก ์ค์ ํ๊ณ ์์ญ์์ ๋ฒ์ด๋๋ ์ด๋ฏธ์ง๋ฅผ ์๋ณด์ด๊ฒ ์ฒ๋ฆฌํ๊ธฐ ์ํด overflow: hidden์ ์ค์ ํด์ค๋๋ค.
2. ์ด๋ฏธ์ง๊ฐ ์ฐ์์ ์ผ๋ก ์์ง์ผ ์ ์๋๋ก ์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ 6๋ฒ์งธ์ ์ถ๊ฐ๋ก ๋ฃ์ต๋๋ค. (์ ๋๋ฉ์ด์
์ค์ ์ ์คํฌ๋ฆฝํธ ํํธ์์!!)
3. ๋ง์ฐฌ๊ฐ์ง๋ก ์ด๋ฏธ์ง๊ฐ 6์ฅ์ด๊ธฐ ๋๋ฌธ์ inner์ ๊ฐ๋ก ํฌ๊ธฐ๋ฅผ +ํ์ฌ 4800px๋ก ์ค์ ํฉ๋๋ค.
4. ๋ท ๋ฉ๋ด ์คํ์ผ์ ์ง์ ํด์ค๋๋ค.
โ๏ธ 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: 4800px; /* ์ด ์ฌ์ฏ๊ฐ์ ์ด๋ฏธ์ง */
height: 450px;
}
.slider { /* ๊ฐ๋ณ์ ์ธ ์ด๋ฏธ์ง (๊ฐ๊ฐ์ ์ด๋ฏธ์ง ์ค์ ) */
position: relative;
width: 800px;
height: 450px;
}
.slider::before {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 5px 10px;
}
.slider:nth-child(1)::before {content: '์ด๋ฏธ์ง6';}
.slider:nth-child(2)::before {content: '์ด๋ฏธ์ง7';}
.slider:nth-child(3)::before {content: '์ด๋ฏธ์ง8';}
.slider:nth-child(4)::before {content: '์ด๋ฏธ์ง9';}
.slider:nth-child(5)::before {content: '์ด๋ฏธ์ง10';}
.slider:nth-child(6)::before {content: '์ด๋ฏธ์ง6';}
@media(max-width : 800px){
.slider__img {
width: 400px;
height: 250px;
}
.slider {
width: 400px;
height: 250px;
}
}
.slider__btn a {
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;
}
.slider__btn a:hover {
border-radius: 50px;
background: rgba(38, 52, 103, 0.4);
}
.slider__btn a.prev {
left: 0;
}
.slider__btn a.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: 2px;
}
.slider__dot .dot.active {
background: rgba(255,255,255,0.9);
}
#2. JAVASCRIPT : ์คํฌ๋ฆฝํธ ์ง๊ธฐ
๋ฐ๋ณต๋ฌธ์ ํตํด ๋ฌธ์์ด์ ์์ฑํ๊ณ ๊ฐ ํจ์๋ฅผ ๋ง๋ค์ด ์ฌํ์ฉ์ด ๊ฐ๋ฅํ๋๋ก ํฉ๋๋ค.
{
const sliderWrap = document.querySelector(".slider__wrap");
const sliderImg = document.querySelector(".slider__img"); //๋ณด์ฌ์ง๋ ์์ญ
const sliderInner = document.querySelector(".slider__inner"); //์์ง์ด๋ ์์ญ
const slider = document.querySelectorAll(".slider"); //๊ฐ๊ฐ์ ์ด๋ฏธ์ง
const sliderBtn = document.querySelector(".slider__btn"); //๋ฒํผ
const sliderBtnPrev= sliderBtn.querySelector(".prev"); // ์ผ์ชฝ๋ฒํผ
const sliderBtnNext = sliderBtn.querySelector(".next"); // ์ค๋ฅธ์ชฝ ๋ฒํผ
const sliderDot = document.querySelector(".slider__dot"); // ๋ท๋ฉ๋ด
let currentIndex = 0; //ํ์ฌ ์ด๋ฏธ์ง
let sliderCount = slider.length; //์ด๋ฏธ์ง ๊ฐฏ์
let sliderWidth = sliderImg.offsetWidth; //์ด๋ฏธ์ง ๊ฐ๋ก๊ฐ
let dotIndex = "";
function intit(){
// dot ๋ง๋ค์ด์ฃผ๊ธฐ!
// slider.forEach(() => {
// dotIndex += "<a href='#' class='dot'>์ด๋ฏธ์ง1</a>";
// });
// dot ๋ง๋ค์ด์ฃผ๊ธฐ!
slider.forEach(() => dotIndex += "<a href='#' class='dot'>์ด๋ฏธ์ง1</a>");
sliderDot.innerHTML = dotIndex;
//์ฒซ๋ฒ์งธ ๋ท ๋ฒํผ ํ์ฑํ ํ์(active)
sliderDot.firstChild.classList.add("active");
}
intit();
//์ด๋ฏธ์ง ์ด๋ ์คํฌ๋ฆฝํธ
function gotoSlider(num){
sliderInner.style.transition = "all 400ms";
sliderInner.style.transform = "translateX(" + -sliderWidth * num + "px)";
currentIndex = num;
//๋๋ฒ์ฌ ์ด๋ฏธ์ง์์ ๋๋ฒ์งธ ๋ท ๋ฒํผ ํ์ฑํ (ํญ ๋ฉ๋ด์ ์๋ฆฌ ๋์ผ)
//1. ๋ท ๋ฉ๋ด์ ํด๋์ค๋ฅผ ๋ชจ๋ ์ญ์
//2. ํด๋น ์ด๋ฏธ์ง์ ํด๋น ๋ท ๋ฉ๋ด์ ํด๋์ค ์ถ๊ฐ
let dotActive = document.querySelectorAll(".slider__dot .dot");
dotActive.forEach(el => el.classList.remove("active"));
dotActive[num].classList.add("active");
}
//๋ฒํผ์ ํด๋ฆญํ ๋ ์ด๋ฏธ์ง ์ด๋ ์๋์ํค๊ธฐ
document.querySelectorAll(".slider__btn a").forEach((btn, index) => {
btn.addEventListener("click", () => {
let prevIndex = (currentIndex + (sliderCount - 1 )) % sliderCount;
let nextIndex = (currentIndex + 1) % sliderCount;
if(btn.classList.contains("prev")){
gotoSlider(prevIndex);
}else{
gotoSlider(nextIndex);
}
});
});
//dot ๋ฉ๋ด๋ฅผ ํด๋ฆญํ ๋ ์ด๋ฏธ์ง ์ด๋ ์๋์ํค๊ธฐ
document.querySelectorAll(".slider__dot .dot").forEach((dot, index) => {
dot.addEventListener("click", () => {
gotoSlider(index);
});
});
//์๋ฐ์คํฌ๋ฆฝํธ๋ ์์๋๋ก ์งํ๋๋ฏ๋ก ์คํ๋ฌธ์ด ๋งจ ๋ฐ์ ์์ด์ ๋์ค์ง ์์ ๊ฒ.
}
#3. ๊ฒฐ๊ณผ ๋ณด๊ธฐ ๐
'EFFECT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[EFFECT] ๊ฒ์ ์ดํํธ01 : ๋ ํธ๋ก st ๊ตฌ์ฑํ๊ธฐ (1) | 2022.10.24 |
---|---|
[EFFECT] ๊ฒ์ ํจ๊ณผ06 : Search Game (1) | 2022.10.24 |
[EFFECT] ํ๋ด๋ ์ค ํจ๊ณผ07 : ํ ์คํธ ํจ๊ณผ (1) | 2022.10.21 |
[EFFECT] ๊ฒ์ ํจ๊ณผ05 : sort(), reverse()๋ฅผ ์ด์ฉํ์ฌ ์ ๋ ฌํ๊ธฐ (0) | 2022.10.17 |
[EFFECT] ๊ฒ์ ํจ๊ณผ 05 : filter()๋ฉ์๋๋ฅผ ํตํด ์ค์๋ ์์ผ๋ก ๊ฒ์ํ๊ธฐ (3) | 2022.09.30 |
๋๊ธ