javascript 헤더 스크롤 이벤트

~_~·2022년 6월 20일
0

html

    <header>헤더 내용입니다.</header>
    <section>컨텐츠 내용입니다.</section>
    <script src="./script.js"></script>

js

const Header = document.querySelector('header')

document.addEventListener('scroll', function(){
    let scroll = window.scrollY
    if(scroll > 50) {
        Header.classList.add('active')
        Header.innerHTML = '스크롤을 내리면 나옵니다.'
    } else {
        Header.classList.remove('active')
        Header.innerHTML = '헤더 내용입니다.'
    }
})

css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

header {
    position: fixed;
    width: 100%;
    background: gray;
    height: 100px;
    line-height: 100px;
    text-align: center;
    font-size: 30px;
    color: #fff;
    font-weight: bold;
    transition: all .5s;
}

header.active {
    background: orange;
}

section {
    height: 1500px;
    position: relative;
}

0개의 댓글