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

  • Home
  • SEARCH
  • 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 6560993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:31:57+00:00 2026-05-25T13:31:57+00:00

In the code below, I’m checking to see if the window is being scrolled

  • 0

In the code below, I’m checking to see if the window is being scrolled past a certain point and if it is, change an element to use fixed position so that it doesn’t scroll off the top of the page. The only problem is that is seems to be HIGHLY client-side-memory intensive (and really bogs down the scrolling speed) because at every single scroll pixel I am updating the style attributes over and over on the element.

Would checking if the attr is already there before attempting to update it make a significant difference? Is there a completely different and more efficient practice to get the same result?

$(window).scroll(function () {
    var headerBottom = 165;
    var fcHeight = $("#pnlMainNavContainer").height();

    var ScrollTop = $(window).scrollTop();
    if (ScrollTop > headerBottom) {
        $("#HeaderContentBuffer").attr("style", "margin-top:" + (fcHeight) + "px;");
        $("#AddFieldsContainer").attr("style", "position:fixed;width:320px;top:70px;left:41px;");
    } else {
        $("#HeaderContentBuffer").attr("style", "margin-top: 0px;");
        $("#AddFieldsContainer").removeAttr("style");
    }
});

As I’m typing this, I notice that StackOverflow.com using the same type of functionality with their yellow “Similar Questions” and “Help” menus on the right hand side of this page. I wonder how they do it.

  • 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-25T13:31:57+00:00Added an answer on May 25, 2026 at 1:31 pm

    One technique you can use is to set a timer on the scroll event and only do the main work when the scroll position hasn’t changed for a short period of time. I use that technique on resize events which have the same issue. You can experiment with what timeout value seems to work right. A shorter time updates with shorter pauses in scrolling and thus may run more often during the scroll, a longer time requires the user to actually pause all motion for a meaningful time. You will have to experiment with what timeout value works best for your purposes and it would be best to test on a relatively slow computer since that’s where the issue of scroll lag would be most pronounced.

    Here’s the general idea how this could be implemented:

    var scrollTimer = null;
    $(window).scroll(function () {
        if (scrollTimer) {
            clearTimeout(scrollTimer);   // clear any previous pending timer
        }
        scrollTimer = setTimeout(handleScroll, 500);   // set new timer
    });
    
    function handleScroll() {
        scrollTimer = null;
        var headerBottom = 165;
        var fcHeight = $("#pnlMainNavContainer").height();
    
        var ScrollTop = $(window).scrollTop();
        if (ScrollTop > headerBottom) {
            $("#HeaderContentBuffer").attr("style", "margin-top:" + (fcHeight) + "px;");
            $("#AddFieldsContainer").attr("style", "position:fixed;width:320px;top:70px;left:41px;");
        } else {
            $("#HeaderContentBuffer").attr("style", "margin-top: 0px;");
            $("#AddFieldsContainer").removeAttr("style");
        }
    }
    

    You may also be able to speed up your scroll function by caching some of the selectors when the scrolling first starts so they don’t have to be recalculated each time. This is one place where the extra overhead of creating a jQuery object each time might not be helping you.


    Here’s a jQuery add-on method that handles the scrolling timer for you:

    (function($) {
        var uniqueCntr = 0;
        $.fn.scrolled = function (waitTime, fn) {
            if (typeof waitTime === "function") {
                fn = waitTime;
                waitTime = 500;
            }
            var tag = "scrollTimer" + uniqueCntr++;
            this.scroll(function () {
                var self = $(this);
                var timer = self.data(tag);
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(function () {
                    self.removeData(tag);
                    fn.call(self[0]);
                }, waitTime);
                self.data(tag, timer);
            });
        }
    })(jQuery);
    

    Working demo: http://jsfiddle.net/jfriend00/KHeZY/

    Your code would then be implemented like this:

    $(window).scrolled(function() {
        var headerBottom = 165;
        var fcHeight = $("#pnlMainNavContainer").height();
    
        var ScrollTop = $(window).scrollTop();
        if (ScrollTop > headerBottom) {
            $("#HeaderContentBuffer").attr("style", "margin-top:" + (fcHeight) + "px;");
            $("#AddFieldsContainer").attr("style", "position:fixed;width:320px;top:70px;left:41px;");
        } else {
            $("#HeaderContentBuffer").attr("style", "margin-top: 0px;");
            $("#AddFieldsContainer").removeAttr("style");
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below is checking performance of three different ways to do same solution.
The code below is outdated in Python 3.0 by being replaced by subprocess.getstatusoutput() .
See code below, for some reason it only works when I put a breakpoint
The code below illustrates the destruct() being called twice. I'd like to know why?
Code below contains certain tags in all four. Image-1 here is the code :
The code below validate two nameserver textbox. As you can see there is redundancy
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
Code below is working well as long as I have class ClassSameAssembly in same
The code below shows a sample that I've used recently to explain the different

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.