Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:36:47+00:00 2026-05-15T02:36:47+00:00

I have a div which, when my page is first loaded, is about 100px

  • 0

I have a div which, when my page is first loaded, is about 100px from the top (it holds some buttons etc. for the page).

When a user scrolls past it, I would like the div to “follow” the user in that it attaches to the top of the screen. When the user returns to the top of the page, I want it back in its original position.

Visualization - xxxxx is the div:

Default (page load)          User vertically scrolled well past it
---------                    ---------
|       |                    |xxxxxxx| < after div reaches top of screen when
|xxxxxxx|                    |       |   page is scrolled vertically, it stays
|       |                    |       |   there
---------                    ---------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:36:48+00:00Added an answer on May 15, 2026 at 2:36 am

    The trick is that you have to set it as position:fixed, but only after the user has scrolled past it.

    This is done with something like this, attaching a handler to the window.scroll event

       // Cache selectors outside callback for performance. 
       var $window = $(window),
           $stickyEl = $('#the-sticky-div'),
           elTop = $stickyEl.offset().top;
    
       $window.scroll(function() {
            $stickyEl.toggleClass('sticky', $window.scrollTop() > elTop);
        });
    

    This simply adds a sticky CSS class when the page has scrolled past it, and removes the class when it’s back up.

    And the CSS class looks like this

      #the-sticky-div.sticky {
         position: fixed;
         top: 0;
      }
    

    EDIT- Modified code to cache jQuery objects, faster now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page in which I have a div. The content from that
I have a html page with a form. The form has Div which gets
In the following HTML page, I have a div, which contains two spans (
I have the below xpath expression //div[@class=post-content]//img which runs on a html page, scanning
I have div which has three buttons as, <div id=buttons> <input id=preview class=ButtonStyle type=submit
I have a div which contains 2 elements. First element is a span and
I have created a web user control (MemberDetails.ascx) which is loaded dynamically on a
I have a div on my home page which uses a jQuery plugin called
I am building my first extension and have problems getting background page loaded scripts
On my web page i have some links like : <div id=toolbarButtons> <a href=actualites/addLink

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.