강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của chaeerup8657
chaeerup8657

câu hỏi đã được viết

Bản sao tương tác trang web của Apple!

Xác định cảnh nào hiện đang hoạt động

씬마다 currentScene 바뀌는 시점이 제각각이예요;

Viết

·

378

1

빨간 테두리도 메인화면에서 브라우저에 선생님처럼 꽉 차게 나오지 않고..

어떤 씬은 맞게 바뀌고 어떤 씬은 따로 바뀌고 제각각인데 왜이런거죠ㅠ

//js코드

(() => {

    let yOffset = 0;
    let prevScrollHeight = 0// 현재 스크롤 위치(yOffset)보다 이전에 위치한 스크롤 섹션들의 스크롤 높이 합
    let currentScene = 0;   // 현재 활성화된 scroll-section


    const sceneInfo = [
        { // currentScene 0
            type: 'sticky',
            heightNum: 5// 브라우저 높이의 5배로 총 scrollHeight 세팅
            scrollHeight: 0,
            objs: {
                container: document.querySelector('#scroll-section-0')
            }
        },
        { // currentScene 1
            type: 'normal',
            heightNum: 5
            scrollHeight: 0,
            objs: {
                container: document.querySelector('#scroll-section-1')
            }
        },
        { // currentScene 2
            type: 'sticky',
            heightNum: 5,
            scrollHeight: 0,
            objs: {
                container: document.querySelector('#scroll-section-2')
            }
        },
        { // currentScene 3
            type: 'sticky',
            heightNum: 5,
            scrollHeight: 0,
            objs: {
                container: document.querySelector('#scroll-section-3')
            }
        }
    ];




    function setLayout() {
        // 각 스크롤 섹션의 높이 세팅
        prevScrollHeight = 0;
        for(let i=0i<sceneInfo.lengthi++) {
            sceneInfo[i].scrollHeight = sceneInfo[i].heightNum * window.innerHeight;
            sceneInfo[i].objs.container.style.height = `${sceneInfo[i].scrollHeight}px`;
        }
    }


    function scrollLoop() {
        prevScrollHeight = 0;
        for(let i=0i<currentScenei++) {
            prevScrollHeight += sceneInfo[i].scrollHeight;
        }

        if (yOffset > sceneInfo[currentScene].scrollHeight + prevScrollHeight) {
            currentScene++;
        }

        if (yOffset < prevScrollHeight) {
            currentScene--;
        }

        console.log(`prevScrollHeight = ${prevScrollHeight}, pageYOffset=${yOffset}, currentScene=${currentScene}`);
    }

    // 리사이징 하면 그에 맞는 높이 다시 세팅
    window.addEventListener('resize'setLayout);

    window.addEventListener('scroll', () => {
        yOffset = window.pageYOffset;
        scrollLoop();
    });


    setLayout();
})();
// css 코드
@charset 'utf-8';

html {
    font-family'Noto Sans KR'sans-serif;
    font-size14px;
}

body {
    overflow-xhidden;
    colorrgb(292931);
    letter-spacing-0.05em;
    backgroundwhite;
}

p {
    line-height1.6;
}

.global-nav {
    positionabsolute;
    top0;
    left0;
    width100%;
    height44px;
    padding0 1rem;
}
.local-nav {
    positionabsolute;
    top45px;
    left0;
    width100%;
    height52px;
    padding0 1rem;
    border-bottom : 1px solid #ddd;
}
.global-nav-links
.local-nav-links {
    max-width1000px;
    displayflex;
    align-itemscenter;
    height100%;
    margin0 auto;
}
.global-nav-links {
    justify-contentspace-between;
}
.global-nav-links a {
    font-size1.2rem;
}
.product-name {
    margin-rightauto;
    font-size1.4rem;
    font-weightbold;
}
.local-nav-links a:not(.product-name) {
    margin-left2em;
    font-size1rem;
}
a {
    color : rgb(292931);
    text-decorationnone;
}
.scroll-section {
    padding-top50vh;
    border3px solid red;
}
#scroll-section-0 h1 {
    font-size4rem;
    text-aligncenter;
}
.main-message {
    displayflex;
    align-itemscenter;
    justify-contentcenter;
    margin5px 0;
    height3em;
    font-size2.5rem;
}
.main-message p {
    line-height1.2;
    font-weightbold;
    text-aligncenter;
}
.main-message small{
    displayblock;
    margin-bottom0.5em;
    font-size1.4rem;
}
#scroll-section-2 .main-message {
    font-size3.5rem;
}
.description {
    max-width1000px;
    margin0 auto;
    padding0 1rem;
    font-size1.2rem;
    color#888;
}
.description strong {
    floatleft;
    margin-right0.2em;
    font-size3rem;
    colorrgb(292931);
}
.desc-message {
    font-weightbold;
    width50%;
    max-width385px;
}
.pin {
    width1px;
    height100px;
    background-colorrgb(292931);
}
.mid-message {
    max-width1000px;
    margin0 auto;
    padding0 1rem;
    font-size2rem;
    color#888;
}
.mid-message strong {
    colorrgb(292931);
}
.canvas-caption {
    max-width1000px;
    margin0 auto;
    padding0 1rem;
    color#888;
    font-size1.2rem;
}
.footer {
    displayflex;
    align-itemscenter;
    justify-contentcenter;
    height7rem;
    background-colortan;
    colorwhite;
}
.sticky-elem {
    displaynone;
    positionfixed;
    top0;
    left0;
    width100%;
}

#show-scene-0 #scroll-section-0 .sticky-elem
#show-scene-1 #scroll-section-1 .sticky-elem
#show-scene-2 #scroll-section-2 .sticky-elem
#show-scene-3 #scroll-section-3 .sticky-elem {
    displayblock;
}


@media (min-width1024px) {
    #scroll-section-0 h1 {
        font-size9vw;
    }
    .main-message{
        font-size4vw;
    }
    .description {
        font-size2rem;
    }
    .description strong{
        font-size6rem;
    }
    #scroll-section-2 .main-message {
        font-size6vw;
    }
    .main-message small {
        font-size2vw;
    }
    .desc-message {
        width20%;
    }
    .mid-message {
        font-size4vw;
    }
    .canvas-caption {
        font-size2rem;
    }
}
웹 디자인HTML/CSS인터랙티브-웹svgjavascript클론코딩

Câu trả lời 3

6

studiomeal님의 프로필 이미지
studiomeal
Người chia sẻ kiến thức

혹시 default.css 연결 하셨나요?
body에 여백이 있는걸 봐서 아마 default.css가 빠진 것 같은데..
default.css에 모든 블럭 요소들의 box-sizing을
box-sizing: border-box;
로 세팅하는 부분이 있거든요~
border-box는 padding과 border까지 width, height 크기에 포함하기 때문에 계산이 정확히 되는거랍니다^^
한번 확인해보시겠어요?

2

chaeerup님의 프로필 이미지
chaeerup
Người đặt câu hỏi

앗 선생님 말씀대로 default 파일이 빠져서 그런거였네요ㅠㅠㅠ감사합니다!!

0

chaeerup님의 프로필 이미지
chaeerup
Người đặt câu hỏi

#scroll-section-* 의 height값에 padding-top 50vh 준것만큼 + 되어서 yOffset이랑 맞지 않는 것 같아요.

예를 들어 각각의 section height가 4000 이라고 치면 한 섹션 당 4000+400+6px(보더값) 으로 4406px 이 지나야 한 섹션이 바뀌는데 (빨간 보더도 4400px을 기준으로 감싸고있음) yOffset은 4000을 height로 인식해서 (실제 section style속성 height도 4000이라 적혀있음ㅠㅠ) 400px을 남겨놓고 currentScene이 바뀝니다.. 한씬 한씬 바뀔때마다 누적되구요.. padding-top값은 height에 포함이 안되는 건가요?ㅠㅠ 어떻게 해결해야 하나요.. 

Hình ảnh hồ sơ của chaeerup8657
chaeerup8657

câu hỏi đã được viết

Đặt câu hỏi