setting div to position: fixed on scroll with jquery causes content below to jump up

kdjernigan

As you scroll down, the top bar goes up and the moment the top bar gets out of sight, the header gets set to position: fixed;. The moment this happens, the content below takes a massive jump upward with 1 scroll wheel click.

I'm assuming its probably something simple I'm overlooking, but wanted to check to see if someone else saw the issue. I've looked through it, but can't really see whats happening right off.

I've set up a jsfiddle to show this code in full.

http://jsfiddle.net/yZVjU/

$(document).ready(function() {
    var s = $("#header");
    var pos = s.position();                    
    $(window).scroll(function() {
        var windowpos = $(window).scrollTop();
        //$("#header_left").html("Distance from top:" + pos.top + "<br />Scroll position: " + windowpos);
        if (windowpos >= pos.top) {
            s.addClass("stick");
        } else {
            s.removeClass("stick"); 
        }
    });
});

CSS:

#header { float: left; width: 100%; height: 60px; background: #fff; }
#header_left { float: left; display: inline; width: 650px; height: 100px; }
#header_right { float: right; display: inline; width: 200px; height: 100px; text-align: right; }


.stick {
    position:fixed;
    top:0px;
    /*_top: expression( ie6 = (document.documentElement.scrollTop + "px") );*/
    z-index: 1000;
}

If you get to the position where the header gets set to fixed, and click up and down, you'll see where the content below is jumping more than 1 click.

Mohammed Joraid

When you set an element to Position Fixed, it stops taking space anymore from the document since it will be like-floating around. Therefore the rest of DOM will jump up as if that element was suddenly deleted.

Add an empty div and before you set Position to Fixed, fill in that div with empty space, or allocate more space for document to occupy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery position DIV fixed at top on scroll

From Dev

Bootstrap Affix Nav Causes Div Below To Jump Up

From Dev

JQuery Scroll with position:fixed CSS

From Dev

setting div to position: fixed on scroll with jquery causes content below to jump up

From Dev

scroll and position: fixed inside overflow: scroll div

From Dev

div image position fixed + scroll

From Dev

HTML/CSS: scroll div without setting position to fixed or absolute

From Dev

jQuery position DIV fixed on scroll

From Dev

css make background image position fixed and scroll content up

From Dev

How to scroll div with page without position: fixed?

From Dev

Get div with fixed position to fill up width

From Dev

JQuery dynamic update div content, jump by scroll position

From Dev

On scroll: move up content div

From Dev

Getting and Setting position of Content with Jquery

From Dev

jQuery position DIV fixed at top on scroll

From Dev

Pushing content below a position:fixed top nav so it's visible

From Dev

Setting up div position

From Dev

After a new fixed position div created scroll bar doesnt show up

From Dev

CSS: style="position: fixed" causes document to "ignore" <div> and overlays content

From Dev

make div (position:fixed) scroll horizontally

From Dev

HTML/CSS: scroll div without setting position to fixed or absolute

From Dev

jQuery scroll up and down on a div

From Dev

How to make position fixed div scroll horizontally?

From Dev

Get div with fixed position to fill up width

From Dev

Event scroll up and fixed header jquery

From Dev

DIV Position Fixed, inner content overlapping

From Dev

Scroll a fixed position div to its bottom and then stop

From Dev

prevent automatically page scroll up when using jquery fadein/out founction on fixed position div

From Dev

position fixed to absolute on scroll make my img jump

Related Related

  1. 1

    jQuery position DIV fixed at top on scroll

  2. 2

    Bootstrap Affix Nav Causes Div Below To Jump Up

  3. 3

    JQuery Scroll with position:fixed CSS

  4. 4

    setting div to position: fixed on scroll with jquery causes content below to jump up

  5. 5

    scroll and position: fixed inside overflow: scroll div

  6. 6

    div image position fixed + scroll

  7. 7

    HTML/CSS: scroll div without setting position to fixed or absolute

  8. 8

    jQuery position DIV fixed on scroll

  9. 9

    css make background image position fixed and scroll content up

  10. 10

    How to scroll div with page without position: fixed?

  11. 11

    Get div with fixed position to fill up width

  12. 12

    JQuery dynamic update div content, jump by scroll position

  13. 13

    On scroll: move up content div

  14. 14

    Getting and Setting position of Content with Jquery

  15. 15

    jQuery position DIV fixed at top on scroll

  16. 16

    Pushing content below a position:fixed top nav so it's visible

  17. 17

    Setting up div position

  18. 18

    After a new fixed position div created scroll bar doesnt show up

  19. 19

    CSS: style="position: fixed" causes document to "ignore" <div> and overlays content

  20. 20

    make div (position:fixed) scroll horizontally

  21. 21

    HTML/CSS: scroll div without setting position to fixed or absolute

  22. 22

    jQuery scroll up and down on a div

  23. 23

    How to make position fixed div scroll horizontally?

  24. 24

    Get div with fixed position to fill up width

  25. 25

    Event scroll up and fixed header jquery

  26. 26

    DIV Position Fixed, inner content overlapping

  27. 27

    Scroll a fixed position div to its bottom and then stop

  28. 28

    prevent automatically page scroll up when using jquery fadein/out founction on fixed position div

  29. 29

    position fixed to absolute on scroll make my img jump

HotTag

Archive