getAttribute() / switch() / :focus / forEach()
carousel 꾏í ěëŁ â
- ꡸ě ęť ěŹě íěľ ę°ě´ëě ęśęł ěŹíě ë¤ëŚę˛ ë°ę˛Źíęł ëě ě§ě§ ě ěź ě˛ěě ěěąíë ę˛ ě¤ě ě´ë° ę˛ ěěëë° ëšěě ě¤íě´ ě í ě ëě
<div class="carousel-image-box"> <img src="/images/image-1.png" class="carousel-image"> </div> <button type="button" class="carousel-arrow right"> <i class="fas fa-arrow-circle-right"></i> </button>
const rightArrow = document.querySelector('.right'); rightArrow.addEventListener('click', function goNext () { var presentImage = document.querySelector('.carousel-image'); if (presentImage.src === '/images/image-1.png') { presentImage.src = '/images/image-2.png'; } else if (presentImage.src === '/images/image-2.png') { presentImage.src = '/images/image-3.png'; } else if (presentImage.src === '/images/image-3.png') { presentImage.src = '/images/image-4.png'; } else if (presentImage.src === '/images/image-4.png') { presentImage.src = '/images/image-5.png'; } else { presentImage.src = '/images/image-1.png'; } });
- ęˇźë° ě¤ë onclickě ę˛ěíë¤ę° ě´ ę¸ě ë°ę˛Źí¨
- ěŹę¸°ě 'í´ëŚí ëë§ë¤ ę° ëłę˛˝' ëśëśě ě˝ëę° ë´ę° ěěě ě´ ęą°ë ę°ě 맼ë˝ě¸ ëŻíë° ě´ęą´ ě¤íě´ ě ë길ë ěŹę¸° ě˝ëě í ě¤í¸ ę°ě ě´ëŻ¸ě§ srcëĄ ë°ężě í ě¤í¸ í´ë´¤ě
- ꡸ëëĄ codepeněě í´ě ě¸ëś ě´ëŻ¸ě§ ë§íŹëĽź ěŹěŠí¨
<img src="https://cdn.pixabay.com/photo/2021/09/07/11/53/car-6603726_960_720.jpg" class="carousel-image"> <button type="button" onclick="goNext()">ë˛íź</button>
function goNext () { var presentImage = document.querySelector('.carousel-image'); if (presentImage.src === 'https://cdn.pixabay.com/photo/2021/09/07/11/53/car-6603726_960_720.jpg') { presentImage.src = 'https://cdn.pixabay.com/photo/2014/09/03/20/15/shoes-434918_960_720.jpg'; } else if (presentImage.src === 'https://cdn.pixabay.com/photo/2014/09/03/20/15/shoes-434918_960_720.jpg') { presentImage.src = 'https://cdn.pixabay.com/photo/2014/05/21/14/54/feet-349687_960_720.jpg'; } else if (presentImage.src === 'https://cdn.pixabay.com/photo/2014/05/21/14/54/feet-349687_960_720.jpg') { presentImage.src = 'https://cdn.pixabay.com/photo/2014/07/10/10/19/steps-388914_960_720.jpg'; } else if (presentImage.src === 'https://cdn.pixabay.com/photo/2014/07/10/10/19/steps-388914_960_720.jpg') { presentImage.src = 'https://cdn.pixabay.com/photo/2015/03/11/21/50/shutters-669296_960_720.jpg'; } else { presentImage.src = 'https://cdn.pixabay.com/photo/2021/09/07/11/53/car-6603726_960_720.jpg'; } }
- 결곟ë ěě ěë¨......(ěŹě¤ ě´ę˛ë ě˛ěě ěëě. ę°ë°ě ë꾏ě issues íě ě¤ëĽę° ë¨ëŠ´ě ěëě´ ě ë길ë ěě ě ëë ęą´ę° íëë° ę˛ěí´ě Back-forward cache ęľŹę¸ ě¤ě ě ë°ężěŁźëęš ëë¤)
- ěë ë ë¤ ëę°ě´ ě´ëŻ¸ě§ęł ëę°ě´ ë˛íźě´ęł src ëśëś íëë§ ë¤ëĽ¸ë° ë´ ęą´ ě ě ëë 깸ęš. srcě ę°ě´ ěš ěë˛ ěŁźěë ěëëě ě°¨ě´ě¸ë°.
- console.log(presentImage.src)뼟 ě¤íěí¤ëŠ´ http: //127.0.0.1:5500/images/image-1.png ëźęł ëŹë¤. ěŚ, srcě ę°ě´ ęˇ¸ëĽ /images/image-1.pngę° ěëëźë ëť.
- ě´ęą¸ëĄ ë°ęż ě¨ěŁźëęš carouselě´ ěëě í긴 íëë° ě´ë ę˛ ě°ëŠ´ ë´ ěť´í¨í°ěěë§ ëłź ě ěě´ě ěëŻ¸ę° ěë¤
function goNext () { var presentImage = document.querySelector('.carousel-image'); if (presentImage.src === 'http://127.0.0.1:5500/images/image-1.png') { presentImage.src = 'http://127.0.0.1:5500/images/image-2.png'; } else if (presentImage.src === 'http://127.0.0.1:5500/images/image-2.png') { presentImage.src = 'http://127.0.0.1:5500/images/image-3.png'; } else if (presentImage.src === 'http://127.0.0.1:5500/images/image-3.png') { presentImage.src = 'http://127.0.0.1:5500/images/image-4.png'; } else if (presentImage.src === 'http://127.0.0.1:5500/images/image-4.png') { presentImage.src = 'http://127.0.0.1:5500/images/image-5.png'; } else { presentImage.src = 'http://127.0.0.1:5500/images/image-1.png'; } }
- í´ëľě í¤ěëë
getAttribute()
ěë¤ âââ x 10000<!-- HTML ě˝ë ěě --> <div class="carousel-image-box"> <img src="/images/image-1.png" class="carousel-image"> </div> <button type="button" class="carousel-arrow right"> <i class="fas fa-arrow-circle-right"></i> </button>
// ě°ě¸Ą ë˛íź í´ëŚ ě đĄ ë¤ě ě´ëŻ¸ě§ ëł´ěŹěŁźę¸° const rightArrow = document.querySelector('.right'); rightArrow.addEventListener('click', function goNext () { var presentImage = document.querySelector('.carousel-image'); if (presentImage.getAttribute('src') === '/images/image-1.png') { presentImage.src = '/images/image-2.png'; } else if (presentImage.getAttribute('src') === '/images/image-2.png') { presentImage.src = '/images/image-3.png'; } else if (presentImage.getAttribute('src') === '/images/image-3.png') { presentImage.src = '/images/image-4.png'; } else if (presentImage.getAttribute('src') === '/images/image-4.png') { presentImage.src = '/images/image-5.png'; } else { presentImage.src = '/images/image-1.png'; } }); // ě˘ě¸Ą ë˛íź í´ëŚ ě đĄ ě´ě ě´ëŻ¸ě§ ëł´ěŹěŁźę¸° const leftArrow = document.querySelector('.left'); leftArrow.addEventListener('click', function goBack () { var presentImage = document.querySelector('.carousel-image'); if (presentImage.getAttribute('src') === '/images/image-1.png') { presentImage.src = '/images/image-5.png'; } else if (presentImage.getAttribute('src') === '/images/image-2.png') { presentImage.src = '/images/image-1.png'; } else if (presentImage.getAttribute('src') === '/images/image-3.png') { presentImage.src = '/images/image-2.png'; } else if (presentImage.getAttribute('src') === '/images/image-4.png') { presentImage.src = '/images/image-3.png'; } else { presentImage.src = '/images/image-4.png'; } });
đĽ ěěą( attribute ) ě°¸ęł
- getAttribute('꡸ ę°ě ěťęł ěśě ěěą ě´ëŚ')
- setAttiribute('ěśę°íęł ěśě ěěą ě´ëŚ', '꡸ ę°')
- hasAttribute('ěĄ´ěŹ ěŹëśëĽź ěęł ěśě ěěą ě´ëŚ')
- removeAttribute('ě ęą°íęł ěśě ěěą ě´ëŚ')
- ę˛°ęľ ě´ë°ě ěěąíë ě˝ëěě
src
뼟getAttribute('src')
ëĄë§ ë°ęž¸ëŠ´ ëë ęą°ěë¤......- ě ęą´ ě´ë ľě§ë ěęł , ë¨ěí 기본 if 쥰깴돸ě ë´ę° ě´í´ë íęł ěë 꾏돸ě¸ë° ëŠě˛í ëë ě ężě ęłłë§ ë체 ëŞ ë°í´ëĽź ë 깸ęš.
- ěźë¨ě 꾏íě´ ëë ę˛ ěëë ě¸ë°ěë ěŹë´ě íźě ěě´ęł , ë¤ě ë¨ęłëĄ ëě´ę°
- ë¨ě ě꾏 ěŹíě
ě´ëŻ¸ě§ íë¨ě Dot뼟 ë뼟 경ě°, í´ëš ěë˛ě ě´ëŻ¸ě§ ëł´ěŹěŁźę¸°
<!-- HTML ěě --> <div class="carousel-dots"> <button class="carousel-dot">â</button> <button class="carousel-dot">â</button> <button class="carousel-dot">â</button> <button class="carousel-dot">â</button> <button class="carousel-dot">â</button> </div>
// CSS ěě .carousel-dot { background-color: transparent; border-style: none; cursor: pointer; font-size: 20px; line-height: 2.5; margin: 0 15px; color: grey; } .carousel-dot:focus { color: black; outline: none; }
// javascript ěśę° - íë¨ě Dot 5ę° const btn_1 = document.querySelectorAll('.carousel-dot')[0]; btn_1.addEventListener('click', function showImage () { presentImage.src = '/images/image-1.png'; }); const btn_2 = document.querySelectorAll('.carousel-dot')[1]; btn_2.addEventListener('click', function showImage () { presentImage.src = '/images/image-2.png'; }); const btn_3 = document.querySelectorAll('.carousel-dot')[2]; btn_3.addEventListener('click', function showImage () { presentImage.src = '/images/image-3.png'; }); const btn_4 = document.querySelectorAll('.carousel-dot')[3]; btn_4.addEventListener('click', function showImage () { presentImage.src = '/images/image-4.png'; }); const btn_5 = document.querySelectorAll('.carousel-dot')[4]; btn_5.addEventListener('click', function showImage () { presentImage.src = '/images/image-5.png'; });
- ë¤ěŻ ę°ě ě ë¤ě ë뼴늴 ę°ę°ě ě´ëŻ¸ě§ę° ëě¨ë¤
- ꡸ë°ë° ě´ęą´ ě ë§ ë돴 ě¤ěŹ ěŁźęł ěśę˛ ě겟ë¤. ěë§ë for 꾏돸ě ě°ëŠ´ ë ęą° ę°ěë° ě ííę˛ ë ě¨ěź í ě§ëĽź ëŞ¨ëĽ´ę˛ ë¤.
- ë¸ëĄęˇ¸ě ě 댏í´ëě for ë°ëłľëŹ¸ë¤ě ë¤ě í ë˛ ě´í´ëł¸ í
forEach 꾏돸
ě ě¨ëł´ę¸°ëĄ íë¤const btns = document.querySelectorAll('.carousel-dot'); btns.forEach(function clickBtn (btn, index) { btns[index].addEventListener('click', function showImage () { presentImage.src = `/images/image-${index+1}.png`; }); });
- ꡸ë°ë° ě´ę˛ ěŹęą¸, ëë¤ëë¤ëë¤ëë¤ëë¤
- ě´ë¤ ę˛ěë ěě´ ě°¨ęˇźě°¨ęˇź íëěŠ ěę°í늴ě ě ě´ëł¸ ęą´ë° ë§íë ëśëśë ěě´ ě˝ë ěěąě´ ëë í ě¤íęšě§ 돸ě ěě´ ë ęą´ ě´ ęłźě 뼟 ěěí ě´ëëĄ ě´ë˛ě´ ě˛ěě´ë¤. ë돴 ě ę¸°í¨ đ˛
- ꡸ë ě§ë§, ě´ęą¸ í´ë¸ ę˛ęłź ëłę°ëĄ ě´ë˛ ęłźě 뼟 í늴ě ëźě ëŚŹę˛ ëë ęą´, ëë for ë°ëłľëŹ¸ě ě 모뼸ë¤ë ęą°ë¤. ě´ ę˛˝ě°ë forEach 꾏돸 ě¤ëŞ ě ě´í´ëł´ëęš 'ě ě§' ěŹę¸° ě ěŠí늴 ë ęą° ę°ěě íëěŠ ě ë¤ëł´ë ë 경ě°ë¤. ěŚ, forEach ęľŹëŹ¸ě´ ě´ëě ë ęą° ę°ë¤ęł ě˝ë뼟 ě 기 ě ëśí° ëŞ ëŁíę˛ ë¨¸ëŚżěě ě 댏í´ě ë§í ěë ěěë¤. ěŹëŹëŞ¨ëĄ ě´ë˛ ęłźě ë ë¤ě íë˛ ě´ě´íę˛ ęłľëśí´ěź í íěěąě ě ě¤íę˛ ëëźëëĄ ë§ë¤ě´ě¤ ęłźě ěë¤.
carousel ěľě˘
ě˝ë
<section>
<div class="title-image-box">
<img src="/images/vanilla_coding_logo.png" />
</div>
<h1>Carousel</h1>
<!-- Carousel Start -->
<div class="carousel-outer">
<div class="carousel-image-and-arrow">
<button type="button" class="carousel-arrow left">
<i class="fas fa-arrow-circle-left"></i>
</button>
<div class="carousel-image-box">
<img src="/images/image-1.png" class="carousel-image">
</div>
<button type="button" class="carousel-arrow right">
<i class="fas fa-arrow-circle-right"></i>
</button>
</div>
<div class="carousel-dots">
<button class="carousel-dot">â</button>
<button class="carousel-dot">â</button>
<button class="carousel-dot">â</button>
<button class="carousel-dot">â</button>
<button class="carousel-dot">â</button>
</div>
</div>
<!-- Carousel End -->
</section>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
width: 100%;
}
body {
background-image: url("./images/bg.jpeg");
background-repeat: no-repeat;
background-size: cover;
font-family: "Varela Round", sans-serif;
}
section {
margin: 60px auto;
max-width: 1000px;
}
.title-image-box {
text-align: center;
padding: 0 80px;
}
.title-image-box img {
width: 100%;
max-width: 800px;
}
h1 {
font-family: "Pacifico", cursive;
text-align: center;
font-size: 5vw;
}
.carousel-outer {
display: flex;
flex-direction: column;
align-items: center;
background-color: white;
margin: 20px auto;
width: 80%;
}
.carousel-image-and-arrow {
display: flex;
justify-content: center;
align-items: center;
margin: 0 auto;
width: 100%;
margin-top: 50px;
}
.carousel-arrow {
background-color: transparent;
border-style: none;
width: 120px;
}
.fa-arrow-circle-left,
.fa-arrow-circle-right {
font-size: 30px;
cursor: pointer;
}
.carousel-image-box {
width: 70%;
margin: 0 auto;
}
.carousel-image {
width: 100%;
vertical-align: middle;
}
.carousel-dots {
text-align: center;
}
.carousel-dot {
background-color: transparent;
border-style: none;
cursor: pointer;
font-size: 20px;
line-height: 2.5;
margin: 0 15px;
color: grey;
}
.carousel-dot:focus {
color: black;
outline: none;
}
var presentImage = document.querySelector('.carousel-image');
// ě°ě¸Ą ë˛íź
const rightArrow = document.querySelector('.right');
rightArrow.addEventListener('click', function goNext () {
const imageSrc = presentImage.getAttribute('src');
switch (imageSrc) {
case '/images/image-1.png':
presentImage.src = '/images/image-2.png';
break;
case '/images/image-2.png':
presentImage.src = '/images/image-3.png';
break;
case '/images/image-3.png':
presentImage.src = '/images/image-4.png';
break;
case '/images/image-4.png':
presentImage.src = '/images/image-5.png';
break;
case '/images/image-5.png':
presentImage.src = '/images/image-1.png';
break;
}
});
// ě˘ě¸Ą ë˛íź
const leftArrow = document.querySelector('.left');
leftArrow.addEventListener('click', function goBack () {
const imageSrc = presentImage.getAttribute('src');
switch (imageSrc) {
case '/images/image-1.png':
presentImage.src = '/images/image-5.png';
break;
case '/images/image-2.png':
presentImage.src = '/images/image-1.png';
break;
case '/images/image-3.png':
presentImage.src = '/images/image-2.png';
break;
case '/images/image-4.png':
presentImage.src = '/images/image-3.png';
break;
case '/images/image-5.png':
presentImage.src = '/images/image-4.png';
break;
}
});
// íë¨ě Dot 5ę° - ex) indexę° 0ě¸ ë˛íźě ë뼟 ë í늴ě image-1 ëě°ę¸°
btns.forEach(function clickBtn (btn, index) {
btns[index].addEventListener('click', function showImage () {
presentImage.src = `/images/image-${index+1}.png`;
});
});
cf. ë°ëĄ ě ě§ ěěě§ë§, ě°ě¸Ą ë˛íźęłź ě˘ě¸Ą ë˛íź ëśëśë if, else if, else 꾏돸ě switch 꾏돸ěźëĄ ë¤ě ě ě ęą´ë° ě˝ëę° ë§ ęˇ¸ë ę˛ ě¤ě´ë ęą° ę°ě§ ěë¤. if ęľŹëŹ¸ëł´ë¤ ëł´ę¸°ë í¸í´ě§ ëŻíě§ë§, ěë§ 200% ë ě¤ěź ě ěě§ ěěęš.
ě´ë ę˛ í´ě ěĽěĽ ę˝ě°Ź 3ěźě 깸ěš
carousel
ë§ë¤ę¸°ę° ëě´ ëŹë¤. ě´ë˛ěë ě¤ë 기ę°ě ęą¸ěł í´ę˛°ě í긴 íě§ë§, ě ě˛ëź ëżëŻíę¸°ëł´ë¨ ě˘ ë íě¤ě ě§ěí ëëě´ë¤.(ëŹźëĄ ě˛ěě ëë ęą° 보늴ě 돟ę°ë°ě ěšę¸´ íě)ěěěë ë§íě§ë§, ě´ë˛ ęłźě 뼟 í´ę˛°íë ęłźě ěě ë´ę° ëŞ¨ëĽ´ęł íˇę°ë ¤ íë ę˛ë¤ě´ ěě ě´ěěźëĄ ë§ë¤ë 깸 ěę˛ ëë¤. ëšě°í ě기ě§ë§, ě´í´í ě˛ ě´ëĄ ě ě 댏íë ę˛ęłź ě§ě§ëĄ ě´í´í 깸 ě§ě ě˝ëëĄ ě¨ëł´ë ęą´ ě˛ě§ ě°¨ě´ě¸ ęą° ę°ë¤.
ě´ëě 본 ęą´ ěě´ě ě´ęą¸ ě°ëŠ´ ë ęą° ę°ěë° ě ëë¤. ëěźëĄ 보늴 ë윊 ěë ę˛ ę°ě§ë§, ě¤ě ë´ ě¤ě¤ëĄ ë´ę° íˇę°ë ¤ íë ëśëśě´ ě´ëě§ëĽź ëŞ ííę˛ íęľě´ëĄ ě기íë ę˛ëśí°ë ě´ë ¤ě´ ęą° ę°ë¤. ę˛ěě í늴ěë ě§ě ě 보뼟 ě ííë ę˛ ěëëź ě´ë°ě ë° ě ëł´ě íëëŚŹę¸°ë§ íë ë뼟 ë°ę˛Źí기ë íë¤.
꡸ëë ëęšě§ íŹę¸°íě§ ěęł , carouselě´ë ë¨ě´ëĽź ě§ě ę˛ěíě§ ěęł í´ë¸ ę˛ë§íźě ëżëŻíë¤. ěźě ě ěĄ°ę¸ ě§ě˛´ëě§ë§, ě¤ę°ě íŹę¸°íęł ë¤ëĽ¸ ěŹëě ëľě ë´¤ë¤ëŠ´ ěě íííě ęą° ę°ë¤. ëě¤ě ë¤ě 보늴 ěě˛ëę˛ íŹë°í ëľě´ę˛ ě§ë§, ě§ę¸ě ëëŚ ě ě
(ě ěŚ)ě´ ë´ę˛¨ ěë ěíëź ęłě ëł´ę˛ ëë ęą° ę°ë¤.ě¸ě ëě ę°ě´ ëľëľíě§ë§, íŹę¸°íě§ ěěě ě미 ěë ěę°ě´ěë¤. ëśęłź ě´ě ë§ í´ë ë¤ě ě´ëĄ ęłľëśëĽź íęł ěśë¤ęł íěëë° ęłźě 뼟 í´ę˛°íęł ëëęš ěě ě¤ěľě´ ë ěŹë°ë ęą° ę°ë¤.
ě´ë ę˛ ëëě´ STEP 4ęšě§ ëě´ ëęł STEP 5뼟 ë¤ě´ę° ě°¨ëĄě´ë¤. ěŹěŠ íě´ ë´ë ě´ë˛ ęłźě 뼟 íŹí¨í´ě ë´ę° ꡸ëě íˇę°ë ¸ë ę˛ë¤ě ë¤ëŁ¨ęł ěë ęą° ę°ë¤. ęˇźë° ě°¸ ě¸í°ëˇ ę˛ěě íľí´ ěëŁëĽź 보늴 ě´í´í ęą° ę°ěë° ë§ě ě ěŠíë ¤ęł í늴 머댏ë ěě´ ě ěě§ě¸ë¤. ě´ë˛ STEPě´ ëěě´ ëěźëŠ´ ě˘ę˛ ë¤.
ěśę° ě´ëĄ ę°ěë ë ëŁęł ěśěë° ěźë¨ě ěŹě íěľ ę°ě´ë뼟 먟ě ë´ěźę˛ ë¤. ë¨ě ě¤ěľě´ 3ę°ě¸ë° 돸ě 뼟 í¸ë ë° ěŁźě´ě§ 기ę°ě´ ěľë 13ěźě´ëźęł ě í ěë¤. ę´í ë¤ëĽ¸ ę°ě뼟 먟ě ë¤ěë¤ę° ě´ęą¸ 몝íę˛ ë늴 ëí¨ëęš ěźë¨ě 죟ě´ě§ ę˛ëśí° í ěę°ě´ë¤.