ํ๋ก ํธ ์๋ ๊ณต๋ถ ์์ ์ดํ ์ฒ์์ผ๋ก ์ฌ๋ฌ ํ์ด์ง๋ฅผ ํ์ ์ผ๋ก ๋ง๋๋ ๊ฒฝํ์ ํ์ต๋๋ค.
์ฒ์์ธ ๋งํผ ๋ง์ ์ํ์ฐฉ์ค๋ค์ ๊ฒช์์ง๋ง ๊ฒฐ๊ณผ๋ฌผ์ด ๋์๋ค๋ ๊ฒ์ ํฐ ๋๊ธฐ๋ถ์ฌ์ ์ฑ์ทจ๊ฐ์ ์ป์๋ค. ๋ํ, ์ด๋ฒ ํ๋ก์ ํธ๋ฅผ ๊ณ๊ธฐ๋ก, ์๋ฉํฑํ ๋งํฌ์ , scss์ ํ์ผ ๋ถ๋ฆฌ ๋ฐ sass ๋ช ๋ น์ด์ js Dom control์ ๋ํ ์ดํด๊ฐ ๋์์ก๋ค.
์ด๋ฐ์๋ ํ ํ๋ก์ ํธ๋ก ์งํํ์ง๋ง, ํ๋ก์ ํธ ์ดํด๋๋ฅผ ๋์ด๊ณ ์ ์ฒด์ ์ธ ํ๋ฆ์ ์๊ธฐ ์ํด ๊ฐ์ธ์ผ๋ก๋ ๋ณํํ์ฌ ์งํํ๋ฉฐ, ํ์๋ค์ด ๋งํ๋ ๋ถ๋ถ๋ค์ ๊ฐ์ด ๊ณ ๋ฏผํ๋ฉฐ ์งํํ๋ค.
๋ง์ ์ด๋ ค์์ด ์์์ง๋ง ๊ทธ ์ค ๊ฐ์ฅ ์๊ฐ์ ๋ง์ด ํ๋ ๋ถ๋ถ์ ๊ฐ feed์ ์ด๋ฏธ์ง ์ฌ๋ผ์ด์ค ๊ธฐ๋ฅ์ ๋ฃ๋ ๊ฒ์ด์๋ค.
์ด๋ฏธ์ง ํ๋จ์ ์บ๋ฌ์
๋ฒํผ์ ๋๋ฅด๋ฉด ์ฌ๋ผ์ด๋๋๋ ๊ธฐ๋ฅ
์ด๋ฐ์ ์์ฑ๋ ์ฝ๋๋ ํ๋ ์ฝ๋ฉ๋ HTML์ ์๋ feed์ ์ด๋ฏธ์ง๋ค์ ์ฌ๋ผ์ด๋๋๊ฒ ๋ง๋ค์ด์ ธ์์ด ๊ธฐ๋ฅ์ ์ผ๋ก๋ ๋ฌธ์ ์์ด ๋ณด์๋ค.
ํ์ง๋ง ์ฌ์ฉ์๋ค์ feed๋ฅผ ๋์ ์ผ๋ก ์์ฑํด ํ๋ฉด์ ๊ทธ๋ ค์ฃผ๋ ๊ณผ์ ์์ ์์์ ์์ฑํ ์ด๋ฏธ์ง์ฌ๋ผ์ด๋ ์ฝ๋๋ ์ต์๋จ ํ๊ฐ์ feed์ ๋ํด์๋ง ์ฌ๋ผ์ด๋ ๊ธฐ๋ฅ์ด ์ ์ฉ๋๊ณ ๋๋จธ์ง๋ ์ ์ฉ์ด ๋์ง ์๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์๋ค. -> (querySelector๋ก ์ก์์คฌ๊ธฐ ๋๋ฌด๋ค)
(feed ์์ฑ์์ ๋ถํฐ ๋น์ทํ ๋ฌธ์ ๋ฐ์ -> ๊ฐ feed์ id๋ฅผ ๋ฃ์ด์ฃผ์ด feed๋ฅผ ์์ฑํด ๋ฌธ์ ํด๊ฒฐ)
querySelector
์ className์ผ๋ก ์ ๊ทผquerySelector
๋์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ feed๋ฅผ ์ก์์ค์ผํ๋ค.querySelectorAll
querySelectorAll
๋ก ํด๋น class๋ฅผ ๋ชจ๋ ์ก์ ๋ค์ ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํด ๋ชจ๋ feed์ ์ด๋ฒคํธ๋ฅผ ๋ฌ์์ค๋ค. ๐
์ด๋ฐ์ ๊ฐ์ฅ ๋จผ์ ๋ ์๊ฐ์ ๋ฐฉ์ 1๋ฒ์ querySelectorAll
๋ก ์ด๋ฒคํธ๋ฅผ ๋ฌ์์ค ์๊ฐ์ ํ์ง๋ง ๋๋ฌด ๋นํจ์จ์ ์ผ ๊ฒ์ด๋ผ๋ ์๊ฐ์ ํ๋ค.
Why. ์๊ณ ๋ฆฌ์ฆ์ ๊ณต๋ถํ๊ธฐ ์์ํ ๋๋ถํฐ ๋ฐ๋ณต๋ฌธ์ ์ต๋ํ ์ ๊ฒ ์ฌ์ฉํ์๋ ๊ฐ์ธ์ ์ธ ์ ๋ ? + ์ฝ๋๊ฐ ๋ชป์๊ฒผ๋ค...
์ฌ์ฉ์๊ฐ ๋ชจ๋ feed๋ฅผ clickํ์ง ์์ ์๋ ์๋๋ฐ, ์ผ์ผํ ๋ชจ๋ ๊ณณ์ ์ด๋ฒคํธ๋ฅผ ๋ฌ๊ณ ์๋ ์๊ฐ์ด ์๊น๋ค๋ ์๊ฐ์ ํ๊ฒ ๋์๋ค..
ํด์ ์ฒ์ฒํ ๋ค์ ์๊ฐ์ ํด๋ณด๋ ์ด๋ฒคํธ ์์์ ํตํด feed๋ฅผ ๊ฐ์ธ๊ณ ์๋ container ์น๊ตฌ์๊ฒ ์ด๋ฒคํธ๋ฅผ ๋ฌ์์ฃผ๊ณ ์ด๋ฒคํธ ์บก์ณ๋ง
๊ณผ ์ด๋ฒคํธ ๋ฒ๋ธ๋ง
์ผ๋ก ํด๋น feed์ ์บ๋ฌ์
๋ฒํผ์ ์ก์์ฃผ๋ฉด ์ด๋จ๊น๋ผ๋ ์๊ฐ์ ํ๊ฒ๋์๋ค.
๊ฒฐ๋ก ๋ถํฐ ๋งํ์๋ฉด ๋ด๊ฐ ์๊ฐํ๋ ๊ฒ ์ด์์ผ๋ก ๋ ๋ค์ํ ์ด๋ฒคํธ์ ๋ํด ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๊ฒ ๋์๋ค.
์ด๋ฐ ์๊ฐ : ์ด๋ฒคํธ ์์์ผ๋ก ๊ฐ feed์ ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋ ๊ธฐ๋ฅ ๋ฃ์ด์ผ์ง
๊ตฌํ ์ดํ ์๊ฐ : ๊ฐ๊ฐ์ feed์ ์ผ์ด๋๋ ๋ชจ๋ ์ด๋ฒคํธ๋ฅผ ์กฐ๊ฑด๋ฌธ์ ํตํด ํ๋ฒ์ ์ฒ๋ฆฌํ ์ ์๋ค?! ๋ค๋ฅธ ํ์ผ๋ค๋ ๋ฆฌํํ ๋งํ๋ฌ ๊ฐ์
event.target
์ ํตํด ์ฌ์ฉ์์ ์ก์
์ด ์ผ์ด๋ Element
์ ๋ฐ๊ณ , ํด๋น Element(์ดํ CurrentNode)์ ๋ถ๋ชจ(CurrentNode.parentElement)์ ๋ถ๋ชจ์ ์ด์ ํ์ (CurrentNode.parentElement.previousElementSibling) ์์๋ค์ ์ฐพ์ ํด๋น ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋ ํจ์์ ๋ฃ์ด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์๋ค.<li id=post-${post.id} class="home-post">
<div class=${slideWrapperClassName}>
<!-- ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋ํ ์์ญ -->
<ul class="slide-list">
<li class="slide">
<img src=${postImageList[0]} alt="ํฌ์คํธ ์ฌ์ง" class="post-img">
</li>
<li class="slide">
<img src=${postImageList[1]} alt="ํฌ์คํธ ์ฌ์ง" class="post-img">
</li>
<li class="slide">
<img src=${postImageList[2]} alt="ํฌ์คํธ ์ฌ์ง" class="post-img">
</li>
</ul>
<!-- ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋ํ ์์ญ ๋ -->
<!-- ์บ๋ฌ์
๋ฒํผ ์์ญ -->
<div class="button-wrapper">
<button type="button" class="on" name="0"></button>
<button type="button" name="1"></button>
<button type="button" name="2"></button>
</div>
<!-- ์บ๋ฌ์
๋ฒํผ ์์ญ ๋ -->
</div>
</li>
// ์ด๋ฏธ์ง ์ฌ๋ผ์ด๋ ์ด๋ ๊ณ์ฐ ํจ์
function moveSlide(num, slideList) {
slideList.style.left = -num * (SLIDE_WIDTH + SLIDE_MARGIN) + "px";
}
function slideAnimation(Node) {
const ControlList = Node.parentElement; // .button-wrapper
const ControlBtnAll = ControlList.querySelectorAll("button"); // button๋ค
const slideList = ControlList.previousElementSibling; // .slide-list
// ์ด์ ์ ์ ํ๋์ด ์๋ ์์น์ ์ด๋ํ ์์น ์ฐพ๊ธฐ
let prevIdx;
let moveIdx;
ControlBtnAll.forEach((el, idx) => {
if (el.className === "on") {
prevIdx = idx;
} else if (+Node.name === idx) {
moveIdx = idx;
}
});
ControlBtnAll[prevIdx].classList.remove("on");
Node.classList.add("on");
moveSlide(moveIdx, slideList);
}
์ด๋ฒ ํ๋ก์ ํธ๋ฅผ ํ๋ฉด์ Dom Control์ ๋ํด ๋ง์ ๊ฒ์ ๋ฐฐ์ธ ์ ์์๋ค. ์ํฉ์ ๋ฐ๋ผ querySelector
์ querySelectorAll
์ ์ ์ ํ๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ๋์๋ค.
๋ํ, element๊ฐ ๊ฐ์ง๊ณ ์๋ ์์ฑ๋ค์ด ์ผ๋ง๋ ๋ค์ํ์ง ์๊ธฐํ ์ ์๊ฒ ๋์์ผ๋ฉฐ, console.dir
์ ํตํด element๋ค์ด ๊ฐ์ง๊ณ ์๋ ์์ฑ๋ค์ ์ฐพ์๋ณด๋ ์ฐ์ต์ ํด์ผ๊ฒ ๋ค๋ ์๊ฐ์ ํ๋ค. console.dir
์ ์ฌ์ฉํด ํ์ ์์๋ฅผ ํ์ํ ์ ์๋ค๋ ๊ฒ์ ๋ชฐ๋๋ค๋ฉด ์๋ง๋ ์์์ ์ ์ํ ๋ฌธ์ ํด๊ฒฐ๋ฐฉ์ 1๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ ๊ฒ ๊ฐ๋ค. ๋คํ์ด ๋ชป์๊ธด ์ฝ๋๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ธฐ๋ถ์ด๊ฐ ์ข๋ค.
querySelectorAll
๋ก ์์๋ค์ ์ก์ผ๋ฉด ํด๋นํ๋ ๊ฐ์ฒด๋ Array
๊ฐ ์๋๋ผ NodeList
์ฌ์ Array
๋ฉ์๋๋ค์ ์ฌ์ฉํ ์ ์์๋ค.
ํ์ง๋ง, JavaScript์ call
ํจ์๋ฅผ ์ด์ฉํด NodeList
๊ฐ์ฒด์์ Array
๋ฉ์๋๋ค์ ์ฌ์ฉํ๋ ๊ฒฝํ์ ํ ์ ์์ด. ๋์ JS ์ดํด๋๊ฐ 1 ์ฆ๊ฐํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ๋ฅ๋ค์ด๋ธ ์คํฐ๋์์ ๊ณต๋ถํ ๋
.call
๋ฉ์๋์.apply
๋ฉ์๋๋ ์ ์๋์ง ์๋ฌธ์ด ๋ค์์ง๋ง ์ด๋ ๊ฒ ํ์ฉ์ ํ๋ ๊ธฐ๋ถ์ด ์ง๋ฆฟํ๋ค. ์ด ๋ง์ ์ฝ๋ฉํ๋ ๊ฒ์ด ์๋๊ฐ ์ถ๋ค ๐ค
async function productAPI(method, productId=false) {
const imageUrl = $previewImage.src;
// ์๋ฒ์ ๋ณด๋ผ product ๋ฐ์ดํฐ ์์ฑ
// $inputList = [input#imageInput, input#productName, input#productPrice, input#productLink]
const product = { "itemImage": imageUrl, };
[].slice.call($inputList, 1).forEach((el) => {
// price์ธ ๊ฒฝ์ฐ ์ ์๋ก ๋ณํ
product[el.name] = (el.name === "price") ? parseInt(el.value) : el.value;
});
}
querySelectorAll
๋ก ์ก๊ณ forEach๋ฅผ ์ฌ์ฉํ ์๊ฐ์ ํ๋ค.Array
์ .slice
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ํ๋ค.querySelectorAll
๋ก ์ก์ ์์์ ๊ฐ์ฒด๋ NodeList
์ด๊ธฐ ๋๋ฌธ์ slice
๋ฉ์๋๊ฐ ์๋ค๊ณ ํ๋ค.call
๋ฉ์๋๊ฐ ๋ ์ฌ๋ผ ์ ์ฉ์ ํด๋ณด๋ ์๋์ด ์๋๋ ๊ฒ์ ๋ณผ ์ ์์๋ค. (์ด๋ฒ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ JS๋ ๊ธ๋ฐฉ ์นํด์ง ์ ์์๋ค. ์ญ์ ์ฝ๋ฉ์ ๋๋ฌด ์ฌ๋ฏธ์๋ ์น๊ตฌ์ธ ๊ฒ ๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ํ์๋ค์ด ๋ชจ๋ฅด๋ ๋ถ๋ถ๋ค์ ๊ฐ์ด ๊ณ ๋ฏผํ๋ฉด์ ํ์ด ๋๊ฐ๋ ๋ ๋ง์ ๊ฒ๋ค์ ๋ฐฐ์ธ ์ ์๋ ์๊ฐ์ด์๋ค.
ํ ๊ฐ์ง ์์ฌ์ด ์ ์ ๋ด๊ฐ ์กฐ๊ธ ๋ ์ํ๊ณ , ์๊ณ ์๋๊ฒ ๋ง์๋ค๋ฉด ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉฐ ์ํ์ฐฉ์ค๋ฅผ ์กฐ๊ธ ๋ ๊ฒช๊ณ ์์ํ๊ฒ ํ ํ๋ก์ ํธ๋ฅผ ์ด์ด๋๊ฐ ์ ์์๋๋ฐ ๋ง์ด ๊ณ ์ํด์ค ํ์๋ค์๊ฒ ๊ณ ๋ง์ด ๋ง์ ๋ฟ์ด๋ค.
์์ผ๋ก ๋ ๋ง์ ๊ฒ๋ค์ ๋ง๋ค์ด๊ฐ๋ฉด์ ์ฑ์ฅํด์ผ๊ฒ ๋ค๋ ์๊ฐ์ด ๋ ๋ค.
๋ค์ ํ๋ก์ ํธ๋ ํญ์ ์๊ฐํ๋ฉด์ ์ฝ๋ฉํ์~~ ํ์ดํ !!