Header Div position fixed on top of page but on load show a div on top of it

Joe Borg

I've been trying to figure out the correct way to do this. Three divs, Advert Div on top, Header Div beneath, and Content Div furthest down. On pg load I intend to have the advert div show, but upon scrolling I intend to keep the header div on position fixed, top:0px i.e. at the top of the page over the content.

Am aware of using position fixed in CSS, yet they way I need it contradicts this attribute since I need the header div to move further up till page top on scroll and stay position fixed there.

Am also aware of the possibility in having all three divs on top of each other, and upon scroll, using jquery I'll hide the advert div and naturally the header div would move up to the top and maintain its position fixed attribute.

any suggestions? JS Fiddle link here for a quick example

Benjamin

Here is the code

$(document).ready(function () {
    var top_fixed;
    if ($('#header-con').length > 0)
    {
        top_fixed = $('#header-con').offset().top;
    }
    $(window).scroll(function () {
        if ($('#header-con').length > 0)
        {
            $('#header-con').toggleClass('fixed', $(window).scrollTop() > top_fixed);
        }
    });
});

DEMO

More Clear CSS

*{
    margin:0;
    padding:0;
}

#advert-con { color:yellow;height:130px;background:blue;width:100%;margin:0px;padding:0px;display:block; }
#header-con { height:170px;background:black;color:#fff!important;margin:0px;padding:0px; }
#content-con { height:700px;background:purple; }

.fixed {
    position: fixed;
    top: 0;
    width:100%;
}

UPDATED DEMO

Making the #advert-con fixed

.container{
    margin-top:130px;
    display:inline-block;
    width:100%;
}
.container:after{
    display:block;
    clear:both;
    content:"";
}

FINAL DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scrolling a div with a header but keeping it fixed at 20px top when header disappears

From Dev

jQuery position DIV fixed at top on scroll

From Dev

CSS Position fixed not fixing header to top

From Dev

Moving div on top of header

From Dev

Bootstrap alert in a fixed floating div at the top of page

From Dev

Revealing A Div Fixed to Page Top After Scrolling 95 Pixels

From Dev

Add a resizable handle to the top of a div with position fixed bottom

From Dev

Position logo top right of div

From Dev

Show div at top of page, hide when scrolling, then show div at bottom of page

From Dev

Fixed position div on top vertical space

From Dev

Is it possible to apply a blur filter to a background-image of a div with position:fixed, from another div rendered on top of it?

From Dev

Scrollable div after fixed top div

From Dev

CSS fixed div top to parent div

From Dev

How to position a div left to another div and top fixed to body?

From Dev

Show div relative to it's parent top position

From Dev

jquery go to top of div or page after show and hide div

From Dev

Div Scroll Position Not Reverting Back to Top When Page Is Changed

From Dev

jQuery position DIV fixed at top on scroll

From Dev

Div is not moving to the top of the page

From Dev

Sticky header slide into fixed position from top

From Dev

Fixed position div on top vertical space

From Dev

Why is the div not a the top of the page?

From Dev

CSS fixed div top to parent div

From Dev

margin-top div causes fixed header div to move down

From Dev

fixed div is getting margin top

From Dev

jquery go to top of div or page after show and hide div

From Dev

How to create a div header angled at top of page

From Dev

Fixed div top of web page broken result

From Dev

How to show div top of the page on pageload?

Related Related

  1. 1

    Scrolling a div with a header but keeping it fixed at 20px top when header disappears

  2. 2

    jQuery position DIV fixed at top on scroll

  3. 3

    CSS Position fixed not fixing header to top

  4. 4

    Moving div on top of header

  5. 5

    Bootstrap alert in a fixed floating div at the top of page

  6. 6

    Revealing A Div Fixed to Page Top After Scrolling 95 Pixels

  7. 7

    Add a resizable handle to the top of a div with position fixed bottom

  8. 8

    Position logo top right of div

  9. 9

    Show div at top of page, hide when scrolling, then show div at bottom of page

  10. 10

    Fixed position div on top vertical space

  11. 11

    Is it possible to apply a blur filter to a background-image of a div with position:fixed, from another div rendered on top of it?

  12. 12

    Scrollable div after fixed top div

  13. 13

    CSS fixed div top to parent div

  14. 14

    How to position a div left to another div and top fixed to body?

  15. 15

    Show div relative to it's parent top position

  16. 16

    jquery go to top of div or page after show and hide div

  17. 17

    Div Scroll Position Not Reverting Back to Top When Page Is Changed

  18. 18

    jQuery position DIV fixed at top on scroll

  19. 19

    Div is not moving to the top of the page

  20. 20

    Sticky header slide into fixed position from top

  21. 21

    Fixed position div on top vertical space

  22. 22

    Why is the div not a the top of the page?

  23. 23

    CSS fixed div top to parent div

  24. 24

    margin-top div causes fixed header div to move down

  25. 25

    fixed div is getting margin top

  26. 26

    jquery go to top of div or page after show and hide div

  27. 27

    How to create a div header angled at top of page

  28. 28

    Fixed div top of web page broken result

  29. 29

    How to show div top of the page on pageload?

HotTag

Archive