๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/๊ตญ๋น„์ง€์›

[D+45] Front-end ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ 2์ผ์ฐจ : ๋„ค๋น„๊ฒŒ์ดํ„ฐ ๋ ˆ์ด์•„์›ƒ ์„ค์ • ๋ฐ section ํฌ๊ธฐ ์„ค์ •

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 11. 23.

 

 

๊ตญ๋น„์ง€์› D+45

Front-end ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ 2์ผ์ฐจ

-  ๋„ค๋น„๊ฒŒ์ดํ„ฐ ๋ ˆ์ด์•„์›ƒ ์„ค์ • ๋ฐ section ํฌ๊ธฐ ์„ค์ • -

 


 

์˜ค๋Š˜ ํ•œ ์ผ

- ๋„ค๋น„๊ฒŒ์ดํ„ฐ ๋ ˆ์ด์•„์›ƒ ์„ค์ •

- section์˜์—ญ์„ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด ํฌ๊ธฐ ์„ค์ •

 

ํŒŒ์ผ ๊ตฌ์„ฑ ๋ฐ ๋‚ด์šฉ

 


• style.css

- ์ƒ๋‹จ ๋„ค๋น„๊ฒŒ์ดํ„ฐ์— ๋Œ€ํ•œ ์Šคํƒ€์ผ ์žฌ์„ค์ • ๋ฐ display๋ฅผ flex๋กœ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

- ๋ณธ๋ฌธ ์˜์—ญ ์‚ฌ์ด์ฆˆ๋ฅผ ์ž„์‹œ๋กœ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

.global-nav {
    position: absolute;
    width: 100%;
    height: 44px;
    left: 0;
    top: 0;
}

.local-nav {
    position: absolute;
    width: 100%;
    height: 52px;
    left: 0;
    top: 45px;
}

.global-nav-links {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    max-width: 1000px;
    margin: 0 auto; 
}

.local-nav-links {
    display: flex;              /* display ๋ชจ๋“œ๋ฅผ flex๋กœ ์„ค์ •ํ•œ๋‹ค. */
    align-items: center;        /* ๋‚ด๋ถ€ ์•„์ดํ…œ์„ ์„ธ๋กœ ์ค‘์•™์ •๋ ฌ */
    justify-content: right;     /* ๋‚ด๋ถ€ ์•„์ดํ…œ์„ ๊ฐ€๋กœ ์šฐ์ธก์ •๋ ฌ */
    height: 100%;
    max-width: 1000px;
    margin: 0 auto;
    border-bottom: 1px solid gray;
}

.local-nav-product-link {
    font-size: 1.5rem;          /* ํฐํŠธ๋Š” rem */
    font-weight: bold;
    margin-right: auto;         /* ๊ฐ€์งˆ์ˆ˜ ์žˆ๋Š” margin์˜ ์ตœ๋Œ“๊ฐ’ : auto */

}

/* Overview Compare Buy */
.local-nav-link {
    margin-left: 2em;           /* margin์„ ์ƒ๋Œ€ํฌ๊ธฐ๋กœ ์„ค์ • - ๊ฐ„๊ฒฉ์€ em */
}

.product-name h1 {
    font-size: 7.5rem;
    text-align: center;
}

.sticky-element {
    display: none;
    position: fixed;
}

.scroll-section {
    border: 1px solid red;
}

 

• main.js

- ๋ณธ๋ฌธ section์ •๋ณด๋“ค์˜ ์ง‘ํ•ฉ ์ •๋ฆฌ ๋ฐ ์‚ฌ์ด์ฆˆ ์„ค์ • ๋ฐ ์œ„์น˜๋ฅผ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

(() => {
    
    let yOffset = 0;

    // section ์ •๋ณด ์ง‘ํ•ฉ
    const sectionSet = [
        // section-0์˜ ์ •๋ณด๋“ค - ๋†’์ด, ๋ฐฐ์ˆ˜, ์—˜๋ฆฌ๋จผํŠธ์ •๋ณด
        {
            height : 0,
            hMultiple : 5,
            objs : {
                container : document.querySelector('#section-0')
            }
        },
        // section-1์˜ ์ •๋ณด๋“ค
        {
            height : 0,
            hMultiple : 5,
            objs : {
                container : document.querySelector('#section-1')
            }
        }
    ];

////////////////////////////////////////////////////////
// ์ผ๋ฐ˜ํ•จ์ˆ˜

// Element์˜ ํฌ๊ธฐ, ์œ„์น˜๋“ฑ์„ ์„ค์ •
const setLayout = function()
{
    // section-0๊ณผ section-1์˜ ๋†’์ด๋ฅผ ์„ค์ •ํ•œ๋‹ค.
    for(let i = 0; i < sectionSet.length; i++)
    {
        sectionSet[i].height = window.innerHeight * sectionSet[i].hMultiple;
        console.log('ํด๋ผ์ด์–ธํŠธ ์ฐฝ ๋†’์ด : ' + window.innerHeight);
        console.log(i + '๋ฒˆ์งธ ์„น์…˜ ๋†’์ด : ' + sectionSet[i].height);
        sectionSet[i].objs.container.style.height = `${sectionSet[0].height}px`;
    }

}



////////////////////////////////////////////////////////
// ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

    // ์Šคํฌ๋กค์€ system์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์œ„ window์— ์ด๋ฒคํŠธ์ถ”๊ฐ€
    window.addEventListener('scroll', () => {
        yOffset = window.scrollY;

        console.log(yOffset);
    });

    // load : ๋ชจ๋“  ์ž‘์—…์ด ๋๋‚ฌ๋‹ค.
    window.addEventListener('load', () => {
        // section-0ํ•˜๊ณ  section-1ํ•˜๊ณ ์˜ ๋†’์ด๋ฅผ ์„ค์ •ํ•œ๋‹ค.
        setLayout();
    })





})();

 


๊ฒฐ๊ณผํ™”๋ฉด

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€