Home > Web Front-end > JS Tutorial > How to Make a Div Stick to the Top of the Screen When Scrolling?

How to Make a Div Stick to the Top of the Screen When Scrolling?

DDD
Release: 2024-11-10 20:54:02
Original
178 people have browsed it

How to Make a Div Stick to the Top of the Screen When Scrolling?

Pinning a Div to the Top of the Screen

To achieve the desired effect, position the div below other content and set its initial position to absolute or relative. Once the page is scrolled enough for the div's top edge to align with the viewport, modify the position to fixed and reset the top value to zero.

CSS:

JavaScript (jQuery):

When the scrollTop exceeds 200, the div becomes fixed and scrolls with the viewport. Scrolling back up removes the fixed class, restoring its original position.

The above is the detailed content of How to Make a Div Stick to the Top of the Screen When Scrolling?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template