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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:18+00:00 2026-05-28T07:17:18+00:00

Not too long ago I asked about setting up a DIV which scrolls with

  • 0

Not too long ago I asked about setting up a DIV which scrolls with the rest of the page. Post can be found here.

I’ve set this up, using the following code:

JS..

jQuery(function ($) {
    var el = $('#sidebar'),
    pos = el.position().top;

    alert(pos);

    $(window).scroll(function() {
        el.toggleClass('fixed', $(this).scrollTop() >= pos);
    });
});

CSS..

/* profile sidebar */
#sidebar>div{ width: 300px; margin-top: 10px; }
#sidebar.fixed>div{position:fixed;top:0}

A copy of the page can be found here. The alert was just some debugging.

The problem is, when you scroll a small amount, #sidebar suddenly appears at the very top of the page. In addition, sometimes as you scroll further down, the sidebar appears – and sometimes it doesn’t.

Any idea what might be causing such seemingly random functionality?

  • 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-28T07:17:19+00:00Added an answer on May 28, 2026 at 7:17 am

    I’m still trying to figure out why it works in the first place in the jsfiddle example, but anyway, I know how to fix it:

     $(window).scroll(function() {
        if($(this).scrollTop() >= pos){
            el.addClass('fixed');
        }else{
            el.removeClass('fixed');
        }
    });
    

    I tested this by unbinding the event you had and replacing it with this code. It seemed to work fine.

    The reason I can’t understand why it works in the example: toggleClass should be constantly adding and removing “fixed” if you have scrolled enough, because the conditional is true (true here means whether to toggle). The constant adding and removing of the fixed class causes the jumpy behavior.

    You can watch this on your page: open up some dev tools (firegubg or Chrome) and watch what happens to your sidebar element.

    [UPDATE]

    Actually, I misread the docs. True means the class should be added (I don’t think the docs are very clear though). Thus… the only way I could explain this is if @dunc was running jQuery v1.2 and the switch was getting ignored completely…

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

Sidebar

Related Questions

I asked a question not too long ago about multi-dimensional and jagged arrays in
Not too long ago, I had a problem which required me to set WinDbg.exe
I've got a few questions about best practices in Python. Not too long ago
I asked a very similar question not too long ago and got some great
I remember, not too long ago, somebody post a link on how to specify
I was asked this question not too long ago, and didn't have a good
This enum used to exist not too long ago. I am upgrading to Castle
I'm no longer getting intellisense in my XAML. Not too long ago, I changed
I built a site for a client not too long ago and built it
I'm reviewing a piece of code I wrote not too long ago, and I

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.