[JavaScript30] ๐Ÿ’ก 22. Follow Along Link Highlighter

์กฐ์ค€ํ˜•ยท2021๋…„ 7์›” 31์ผ
0

JavaScript30

๋ชฉ๋ก ๋ณด๊ธฐ
22/30

๐Ÿ’ก 22. Follow Along Link Highlighter

ํ‘œ์‹œ ๋˜์žˆ๋Š” ๋งํฌ์— ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ฆฌ๋ฉด ํšจ๊ณผ๋ฅผ ์ฃผ๋Š” ๋ฐฉ๋ฒ•.

์ดˆ๊ธฐ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€Follow Along Nav</title>
    <link rel="stylesheet" href="style_JuneHyung.css">
</head>
<body>
    <nav>
        <ul class="menu">
            <li><a href="">Home</a></li>
            <li><a href="">Order Status</a></li>
            <li><a href="">Tweets</a></li>
            <li><a href="">Read Our History</a></li>
            <li><a href="">Contact Us</a></li>
        </ul>
    </nav>
    <div class="wrapper">
        <p>Lorem ipsum dolor sit amet, <a href="">consectetur</a> adipisicing elit. Est <a href="">explicabo</a> unde natus necessitatibus esse obcaecati distinctio, aut itaque, qui vitae!</p>
        <p>Aspernatur sapiente quae sint <a href="">soluta</a> modi, atque praesentium laborum pariatur earum <a href="">quaerat</a> cupiditate consequuntur facilis ullam dignissimos, aperiam quam veniam.</p>
        <p>Cum ipsam quod, incidunt sit ex <a href="">tempore</a> placeat maxime <a href="">corrupti</a> possimus <a href="">veritatis</a> ipsum fugit recusandae est doloremque? Hic, <a href="">quibusdam</a>, nulla.</p>
        <p>Esse quibusdam, ad, ducimus cupiditate <a href="">nulla</a>, quae magni odit <a href="">totam</a> ut consequatur eveniet sunt quam provident sapiente dicta neque quod.</p>
        <p>Aliquam <a href="">dicta</a> sequi culpa fugiat <a href="">consequuntur</a> pariatur optio ad minima, maxime <a href="">odio</a>, distinctio magni impedit tempore enim repellendus <a href="">repudiandae</a> quas!</p>
    </div>

    <script>
      // ๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€
    </script>
</body>
</html>

์ดˆ๊ธฐํ™”๋ฉด

๐ŸŒ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ๊ฒƒ

๐Ÿ‘‰ getBoundingClientRect()

์š”์†Œ์˜ ํฌ๊ธฐ์™€ ๋ทฐํฌํŠธ์— ๋Œ€ํ•œ ์œ„์น˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” DOMRect ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ 

https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

๐ŸŒ ๊ณผ์ •

๐Ÿ‘‰ 1. ์ƒ์ˆ˜ ์ƒ์„ฑ

const triggers = document.querySelectorAll('a');
const highlight = document.createElement('span');
highlight.classList.add('highlight');
document.body.append(highlight);

aํƒœ๊ทธ์™€ spanํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด highlightํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•จ.

๐Ÿ‘‰ 2. ํšจ๊ณผ์ ์šฉ ํ•จ์ˆ˜

function highlightLink(){
    // console.log(this);
    const linkCoords = this.getBoundingClientRect();
    console.log(linkCoords);

    const coords = {
        width: linkCoords.width,
        height: linkCoords.height,
        top: linkCoords.top + window.scrollY,
        left: linkCoords.left + window.scrollX
    };

    highlight.style.width = `${coords.width}px`;
    highlight.style.height = `${coords.height}px`;
    highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;

}

linkCoords๋ฅผ console๋กœ ์ฐ์œผ๋ฉด ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋‚˜์˜จ๋‹ค.

highlight์˜ width์™€ height๋ฅผ linkCoords์˜ width, height๋กœ ์ง€์ •ํ•˜๊ณ , top,left๋ฅผ ์„ค์ •ํ•˜๋ฉด scroll Down์‹œ ์ด์ƒํ˜„์ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค.

์ด์ƒํ˜„์ƒ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์Šคํฌ๋กค ์‹œ top๊ณผ left์— scrollY, X๊ฐ’์„ ๋”ํ•ด ์œ„์น˜๋ฅผ ์ˆ˜์ •ํ•จ.

profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€