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 1013871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:10:12+00:00 2026-05-16T10:10:12+00:00

Fairly new to jQuery and JavaScript in general. I mocked up an example of

  • 0

Fairly new to jQuery and JavaScript in general. I mocked up an example of my problem at http://jsbin.com/alibi3/2/ – with an explanation below.

I have a div that, after a user scrolls past a certain point on the page, is assigned a class of “fixed” so it follows the user down the page. This works fine on its own.

Problem is, the content above that div can be toggled to show/hide – and when it is shown, the fixed class is still being applied at the point it would have been if it was hidden, so it appears to ‘jump’.

How do I tell my fixed-class-adding function that the div above has been shown/hidden, and so adjust the point at which the ‘fixed’ class is added?

Thanks.

HTML:

<div id="drawer">
    <a href="#">Click here to toggle drawer</a>
    <p id="drawercontents">Here is the stuff in the drawer; hidden by default.</p>
</div>
<div id="article">
    blah, blah...
</div>
<div id="nav">
    This should follow down the page once we scroll past the start of the article,
    and 'stick' back in place when we are back at the top.
</div>

CSS:

  #article {
    width: 400px;
    float: left;
    margin-right: 20px;
    padding: 20px;
    background: #eee;
  }
  #nav {
    width: 200px;
    float: left;
    background: #ff0;
    padding: 20px;
  }
  #drawer {
    width: 660px;
    padding: 20px;
    color:#fff;
    background: #000;
    margin-bottom: 10px;
  }
  .fixed { position: fixed; left: 460px; top: 0px; }

JavaScript:

$(document).ready(function() {
  $('#drawercontents').hide();

  $('#drawer a').click(function(e){
    e.preventDefault();
    $('#drawercontents').toggle('fast');
  });

  var top =$('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0));
  $(window).scroll(function () {
    // what is the y position of the scroll?
    var y = $(window).scrollTop();    
    // whether that's below the start of article?
    if (y >= top) {
      // if so, add the fixed class
      $('#nav').addClass('fixed');
    } else {
      // otherwise, remove it
      $('#nav').removeClass('fixed');
    }
  });
});
  • 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-16T10:10:12+00:00Added an answer on May 16, 2026 at 10:10 am

    Whenever, you do something that modifies the base position of that “fixed” div, you need to re-snapshot its base position.

    For example, in your demo code, remeasure the top inside the toggle() call.
    See the modified code, below, or see it in action at http://jsbin.com/alibi3/8 .

    var GblTop;
    
    function GetVertOffset (srchStr)
    {
        GblTop = $(srchStr).offset().top - parseFloat($(srchStr).css('marginTop').replace(/auto/, 0));
    }
    
    
    $(document).ready(function ()
    {
        $('#drawercontents').hide();
    
        $('#drawer a').click (function (e)
        {
            e.preventDefault();
            $('#drawercontents').toggle('fast', function() {GetVertOffset ('#nav'); } );
        });
    
    
        GetVertOffset ('#nav');     //-- Sets GblTop
    
        $(window).scroll (function ()
        {
            // what is the y position of the scroll?
            var y = $(window).scrollTop();
            // whether that's below the start of article?
            if (y >= GblTop)
            {
                // if so, add the fixed class
                $('#nav').addClass('fixed');
            }
            else
            {
                // otherwise, remove it
                $('#nav').removeClass('fixed');
            }
        });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to jQuery and JavaScript in general. I have a form,
I'm fairly new to JQuery and Javascript in general. I don't think I'm trying
Fairly new to jQuery, trying to use Autocomplete. Went to http://jqueryui.com/download to build my
I am fairly new to Jquery and javascript and have jumped in head first
Fairly new to Jquery here.... but one thing I have been told and being
I'm fairly new to facebook development, but have experience with PHP and JavaScript. I've
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js type=text/javascript></script> I want to use the above ScriptTag in a SharePoint Visual
I'm fairly new to JavaScript/jQuery/AJAX, so I suspect the issue is some typo I'm
I'm fairly new to Javascript/JQuery. I'm trying to write an app that takes text
I am fairly new to jQuery/JavaScript so I am having a few problems, hopefully

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.