프런트엔드-자바스크립트

자바스크립트 핵심

강사: 정프런트 · 작성자: 이종춘 · 작성일: 2026-06-11 21:21:06 · 조회수: 13

문제 7 : 스크롤 시 header 변경
목록으로
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제7</title>

<style>

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

header{
width:100%;
height:80px;
background-color:white;

display:flex;
justify-content:center;
align-items:center;

font-size:30px;
transition:0.3s;
}

/* active 붙었을 때 */

.header_active{
background-color:black;
color:white;

position:fixed;
top:0;
left:0;

z-index:10;
}

section{
height:100vh;
border:2px solid #ddd;

display:flex;
justify-content:center;
align-items:center;

font-size:50px;
}

.section_1{
background-color:lightgray;
}

.section_2{
background-color:lightblue;
}

.section_3{
background-color:lightpink;
}

</style>
</head>

<body>

<header>
Header
</header>

<section class="section_1">
Section One
</section>

<section class="section_2">
Section Two
</section>

<section class="section_3">
Section Three
</section>

<script>

const headerEl = document.querySelector('header');

const handleScroll = () => {

const pageY = window.scrollY;

if(pageY >= 300){

headerEl.classList.add('header_active');

}else{

headerEl.classList.remove('header_active');

}

}

window.addEventListener('scroll', handleScroll);

</script>

</body>
</html>
#문제 7 : 스크롤 시 header 변경

첨부 파일

0개
첨부된 파일이 없습니다.

댓글 / 답글

0개
댓글을 작성하려면 로그인하세요.
아직 댓글이 없습니다.