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 div in which a page is loaded with the DojoX Layout
I have one aspx page with some controls. Also i have one DIV which
I have small page which has label, DropDownList and a submit button. <div> <asp:label
We have some JavaScript code that resizes <div/> elements (adjusts height/width/padding/margin etc.) based on
I have this html page which globally exists of div , section , and
I have a Div Tag which contains 4 child Div Tags <Div id=Parent> <div
I have a div with overflow:hidden , inside which I show a phone number
I have an absolutely positioned div containing several children, one of which is a
I have a div and an iframe on the page the div has z-index:
So I have my own webpage here , which is a sortable thumbnails page.

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.