728x90
๋ฐ์ํ
Javascript Slider Effect
Slider ์ดํํธ ์ค ๋ฒํผ์ ๋๋ฌ ์ด๋ฏธ์ง๋ฅผ ์์ผ๋ก ๋๊ธฐ๋ ํจ๊ณผ์ ๋๋ค.
HTML
html ์์ค์ ๋๋ค.
<main id="main">
<section id="sliderType02">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider__inner">
<div class="slider" role="group" aria-label="1/5"><img src="../effect/img/effect_sub01-min.jpg" alt="์ฌ๋ผ์ด๋ ์ด๋ฏธ์ง1"></div>
<div class="slider" role="group" aria-label="2/5"><img src="../effect/img/effect_sub02-min.jpg" alt="์ฌ๋ผ์ด๋ ์ด๋ฏธ์ง2"></div>
<div class="slider" role="group" aria-label="3/5"><img src="../effect/img/effect_sub03-min.jpg" alt="์ฌ๋ผ์ด๋ ์ด๋ฏธ์ง3"></div>
<div class="slider" role="group" aria-label="4/5"><img src="../effect/img/effect_sub04-min.jpg" alt="์ฌ๋ผ์ด๋ ์ด๋ฏธ์ง4"></div>
<div class="slider" role="group" aria-label="5/5"><img src="../effect/img/effect_sub05-min.jpg" alt="์ฌ๋ผ์ด๋ ์ด๋ฏธ์ง5"></div>
</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>
</section>
</main>
CSS
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::before {
position: absolute;
left: 5px;
top: 5px;
background-color: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 5px 10px;
}
.slider img {
width: 100%;
}
.slider__inner {
/* ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ธ๊ณ ์๋ ๋ถ๋ชจ : ์์ง์ด๋ ๋ถ๋ถ */
display: flex;
flex-wrap: wrap;
width: 4800px;
/*์ด ์ด๋ฏธ์ง 6๊ฐ */
height: 450px;
}
.slider {
/* ๊ฐ๋ณ์ ์ธ ์ด๋ฏธ์ง */
position: relative;
width: 800px;
height: 450px;
}
.slider:nth-child(1)::before {
content: '์ด๋ฏธ์ง1';
}
.slider:nth-child(2)::before {
content: '์ด๋ฏธ์ง2';
}
.slider:nth-child(3)::before {
content: '์ด๋ฏธ์ง3';
}
.slider:nth-child(4)::before {
content: '์ด๋ฏธ์ง4';
}
.slider:nth-child(5)::before {
content: '์ด๋ฏธ์ง5';
}
.slider:nth-child(6)::before {
content: '์ด๋ฏธ์ง1';
}
@media (max-width: 800px) {
.slider__img {
width: 400px;
height: 225px;
}
}
/* slide btn */
.slider__btn a{
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background: rgba(0,0,0,0.3);
text-align: center;
line-height: 45px;
transition: all 0.2s;
display: block;
color: #fff;
}
.slider__btn a:hover {
background: rgb(255, 71, 71);
border-radius: 40%;
}
.slider__btn a.prev {
left: 0;
}
.slider__btn a.next {
right: 0;
}
Javascript
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 = document.querySelector(".prev"); //์ผ์ชฝ๋ฒํผ
const sliderBtnNext = document.querySelector(".next"); //์ค๋ฅธ์ชฝ๋ฒํผ
let currentIndex = 0; //ํ์ฌ ์ด๋ฏธ์ง
let sliderCount = slider.length; //์ด๋ฏธ์ง ๊ฐฏ์
let sliderWidth = sliderImg.offsetWidth //์ด๋ฏธ์ง ๊ฐ๋ก๊ฐ
//์ด๋ฏธ์ง ์์ง์ด๋ ์์ญ
function gotoSlider (num) {
sliderInner.style.transition = "all 400ms"
sliderInner.style.transform = "translateX(" + -sliderWidth * num + "px)";
currentIndex = num;
};
//์ผ์ชฝ ๋ฒํผ ํด๋ฆญ(์ฃผ๋ง์์ )
sliderBtnPrev.addEventListener("click", ()=>{
let prevIndex = (currentIndex + (sliderCount-1)) % sliderCount
//4 3 2 1 0
gotoSlider(prevIndex);
});
//์ค๋ฅธ์ชฝ ๋ฒํผ ํด๋ฆญ
sliderBtnNext.addEventListener("click", ()=>{
let nextIndex = (currentIndex + 1) % sliderCount
gotoSlider(nextIndex);
// 0 1 2 3 4
});
728x90
๋ฐ์ํ
'์ดํํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ง์ฐ์ค ์ดํํธ 3. ์กฐ๋ช ํจ๊ณผ(์งํ์ค) (1) | 2022.09.22 |
---|---|
ํจ๋ด๋์ค ์ดํํธ 5. ์ด์ง๊ฐ ํจ๊ณผ (์งํ์ค) (2) | 2022.09.20 |
๋ง์ฐ์ค ์ดํํธ 2. ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ (GSAP) (3) | 2022.09.19 |
ํจ๋ด๋์ค ์ดํํธ 4. ๋ด์ฉ ๋ํ๋๊ธฐ (3) | 2022.09.19 |
ํจ๋ด๋์ค ์ดํํธ 3. ๋ฉ๋ด ๋ํ๋๊ธฐ / ํ๋ฒํผ (4) | 2022.09.19 |
๋๊ธ