여러분이 사용하고 계신 브라우저는 HTML5를 지원하지 않기 때문에 몇몇 요소가 제대로 보이도록 JScript를 사용하고 있습니다. 하지만 여러분의 브라우저 설정에서 스크립트 기능이 꺼져있으므로, 현재 페이지를 제대로 확인하시려면 스크립트 기능을 켜주셔야 합니다. How To - 스크롤 시 메뉴 숨기기
How To – 스크롤 시 메뉴 숨기기
3개월전 작성
3개월전 수정

w3.css

스크롤 시 메뉴 숨기기

CSS 및 JavaScript를 사용하여 아래로 스크롤할 때 탐색 메뉴를 숨기는 방법을 알아보자.

아래로 스크롤할 때 Navbar를 숨기는 방법

1단계) HTML 추가

탐색 모음을 만든다.

<div id="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>
2단계) CSS 추가

탐색 모음 스타일을 지정한다.

#navbar {
  background-color: #333; /* Black background color */
  position: fixed; /* Make it stick/fixed */
  top: 0; /* Stay on top */
  width: 100%; /* Full width */
  transition: top 0.3s; /* Transition effect when sliding down (and up) */
}

/* Style the navbar links */
#navbar a {
  float: left;
  display: block;
  color: white;
  text-align: center;
  padding: 15px;
  text-decoration: none;
}

#navbar a:hover {
  background-color: #ddd;
  color: black;
}
3단계) ​​자바스크립트 추가
/* When the user scrolls down, hide the navbar. When the user scrolls up, show the navbar */
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.top = "0";
  } else {
    document.getElementById("navbar").style.top = "-50px";
  }
  prevScrollpos = currentScrollPos;
}
기본 예시
예제 보기
참고
Mingg`s Diary
밍구
공부 목적 블로그