728x90
๋ฐ์ํ
์ฌ๋ผ์ด๋ ์ดํํธ 03
์ฌ๋ผ์ด๋ ์ดํํธ ์ธ ๋ฒ์งธ, ์ข๋ก ์์ง์ด๊ธฐ(์ฐ์์ ์ผ๋ก) ์ ๋๋ค.
HTML
html ์์ค์ ๋๋ค.
<section id="sliderType02">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider__inner">
<div class="slider"><img src="../effect/img/effect_sub01-min.jpg" alt=""></div>
<div class="slider"><img src="../effect/img/effect_sub02-min.jpg" alt=""></div>
<div class="slider"><img src="../effect/img/effect_sub03-min.jpg" alt=""></div>
<div class="slider"><img src="../effect/img/effect_sub04-min.jpg" alt=""></div>
<div class="slider"><img src="../effect/img/effect_sub05-min.jpg" alt=""></div>
</div>
</div>
</div>
</section>
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;
}
}
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"); //๊ฐ๊ฐ์ ์ด๋ฏธ์ง
let currentIndex = 0; //ํ์ฌ ๋ณด์ด๋ ์ด๋ฏธ์ง
let sliderCount = slider.length; //์ด๋ฏธ์ง ๊ฐฏ์
let sliderWidth = sliderImg.offsetWidth; //์ด๋ฏธ์ง ๊ฐ๋ก๊ฐ
let sliderClone = sliderInner.firstElementChild.cloneNode(true); //์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง ๋ณต์ฌ
sliderInner.appendChild(sliderClone); //์ฒซ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ง์ง๋ง์ ๋ฃ์ด์ค
function sliderEffect() {
currentIndex++;
sliderInner.style.transition = "all 0.6s";
sliderInner.style.transform = "translateX(-" + sliderWidth * currentIndex + "px)";
// sliderInner.style.transform = "translateX(-800px)"; 800 * 1
// sliderInner.style.transform = "translateX(-1600px)"; 800 * 2
// sliderInner.style.transform = "translateX(-2400px)"; 800 * 3
// sliderInner.style.transform = "translateX(-3200px)"; 800 * 4
// sliderInner.style.transform = "translateX(-4000px)"; 800 * 5
// sliderInner.style.transform = "translateX(-4800px)"; 800 * 6
// ๋ง์ง๋ง ์ฌ์ง์ ์์นํ์ ๋
if (currentIndex == sliderCount) {
setTimeout(() => {
sliderInner.style.transition = "0s";
sliderInner.style.transform = "translateX(0px)";
}, 700);
currentIndex = 0;
}
}
setInterval(sliderEffect, 2000);
728x90
๋ฐ์ํ
'์ดํํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ๋ด๋ ์ค ์ดํํธ - 1. ๋ฉ๋ด ์ด๋ (6) | 2022.09.06 |
---|---|
๋ง์ฐ์ค ์ดํํธ - ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ (1) | 2022.09.06 |
03. ์ฌ๋ผ์ด๋ ์ดํํธ - ์ข๋ก ์์ง์ด๊ธฐ (5) | 2022.08.29 |
03. ์ฌ๋ผ์ด๋ ์ดํํธ - ํธ๋์ง์ ํจ๊ณผ (6) | 2022.08.29 |
ํด์ฆ ์ดํํธ ๊ฐ๊ด์ ์ฌ๋ฌ ๋ฌธ์ 2 (10) | 2022.08.24 |
๋๊ธ