


How to use JavaScript to achieve the scrolling and hiding effect of the fixed navigation bar at the top of the web page?
How to use JavaScript to achieve the scrolling and hiding effect of the fixed navigation bar at the top of the web page?
In today’s Internet era, web design often focuses on user experience and the integrity of page functions. The web navigation bar is the bridge between users and the website, so the navigation bar is generally placed at the top of the page to facilitate users to quickly find the information they need. However, when users are browsing the web, keeping the navigation bar displayed at the top for a long time may occupy page space and make users feel inconvenient. Therefore, in order to improve the user experience, we can use JavaScript to implement the scrolling and hiding effect of the fixed navigation bar at the top of the web page.
The way to achieve this effect is to listen to scroll events and judge the display and hiding of the navigation bar based on the scroll direction and scroll distance. The following is a sample code:
// 获取导航栏元素 const navBar = document.querySelector('.navbar'); // 定义初始滚动位置 let lastScrollTop = 0; // 定义初始导航栏高度 const navBarHeight = navBar.offsetHeight; // 监听滚动事件 window.addEventListener('scroll', function() { // 获取当前滚动位置 const scrollTop = window.pageYOffset || document.documentElement.scrollTop; // 判断滚动方向 if (scrollTop > lastScrollTop) { // 向下滚动,隐藏导航栏 navBar.style.transform = `translateY(-${navBarHeight}px)`; } else if (scrollTop < lastScrollTop) { // 向上滚动,显示导航栏 navBar.style.transform = 'translateY(0)'; } // 更新滚动位置 lastScrollTop = scrollTop; });
The above code first obtains the navigation bar element with the .navbar
class name through document.querySelector('.navbar')
. Next, we define a variable lastScrollTop
to store the last scroll position, and use navBar.offsetHeight
to obtain the height of the navigation bar.
Then, we listen to the scrolling event through window.addEventListener('scroll', function() { ... })
. In the callback function of the scroll event, we first get the current scroll position scrollTop
. Next, by judging the size relationship between the current scroll position and the last scroll position, we can determine the direction of the scroll.
If the current scroll position is greater than the last scroll position, it means that the user scrolls down, and we hide the navigation bar upwards. It should be noted that we move the navigation bar by setting the translateY
property of navBar.style.transform
, and use the height of the navigation bar (navBarHeight
) as Displacement reference to ensure the navigation bar is completely hidden.
On the contrary, if the current scroll position is smaller than the last scroll position, it means the user scrolled up and we will redisplay the navigation bar.
Finally, we need to update the scroll position lastScrollTop
for comparison in the next scroll event.
Through the above code example, we can achieve the scrolling and hiding effect of the fixed navigation bar at the top of the web page, thereby improving the user's browsing experience. Of course, according to actual needs, we can make some adjustments and improvements to the style and effect of the navigation bar to achieve better user interaction effects.
The above is the detailed content of How to use JavaScript to achieve the scrolling and hiding effect of the fixed navigation bar at the top of the web page?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to send web pages to the desktop as a shortcut in Edge browser? Many of our users want to display frequently used web pages on the desktop as shortcuts for the convenience of directly opening access pages, but they don’t know how to do it. In response to this problem, the editor of this issue will share the solution with the majority of users. , let’s take a look at the content shared in today’s software tutorial. The shortcut method of sending web pages to the desktop in Edge browser: 1. Open the software and click the "..." button on the page. 2. Select "Install this site as an application" in "Application" from the drop-down menu option. 3. Finally, click it in the pop-up window

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

The browser cannot open the web page but the network is normal. There are many possible reasons. When this problem occurs, we need to investigate step by step to determine the specific cause and solve the problem. First, determine whether the webpage cannot be opened is limited to a specific browser or whether all browsers cannot open the webpage. If only one browser cannot open the web page, you can try to use other browsers, such as Google Chrome, Firefox, etc., for testing. If other browsers are able to open the page correctly, the problem is most likely with that specific browser, possibly

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to solve the problem of web pages not opening With the rapid development of the Internet, people increasingly rely on the Internet to obtain information, communicate and entertain. However, sometimes we encounter the problem that the web page cannot be opened, which brings us a lot of trouble. This article will introduce you to some common methods to help solve the problem of web pages not opening. First, we need to determine why the web page cannot be opened. Possible reasons include network problems, server problems, browser settings problems, etc. Here are some solutions: Check network connection: First, we need

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Executing PHP code in a web page requires ensuring that the web server supports PHP and is properly configured. PHP can be opened in three ways: * **Server environment:** Place the PHP file in the server root directory and access it through the browser. * **Integrated Development Environment: **Place PHP files in the specified web root directory and access them through the browser. * **Remote Server:** Access PHP files hosted on a remote server via the URL address provided by the server.
