rodge
rodge

Reputation: 264

Sticky menu quickly jump to the top while header is not fully hidden or scroll to top

I am trying to create a sticky navigation using jquery. My navigation quickly jump to the top while the header is not yet fully hidden or scrolled to the top. I want the header to be fully scrolled to the top before the navigation stick to its position.

question: How can I make the navigation stick to its position after the header flow is completely hidden to the top after scrolling?

This is my jsfiddle link http://jsfiddle.net/rodgefhi/577br99o/ illustration.

This is my code

<style type="text/css">
    .clearer {
        clear: both;
        margin: 0;
        padding: 0;
    }
    .header {
        width: 84%;
            margin: 0 auto;
    }
    .dummy-content {
        width: 10%;
        margin: auto;
    }
    .fixed {
        position: fixed;
        top: 0;
    }
    .nav-placeholder {
        width: 84%; 
        margin: 0 auto;
        background: transparent;
        height: auto;
    }
    nav {
        width: 84%;
        background: red;
        }
        nav ul li {
            list-style: none;
            float: left;
            padding: 10px;
            text-align: center;
            }
            nav ul li:hover {
                cursor: pointer;
                background: white;
            }
</style>
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var navOffset = $("nav").offset().left;

        $("nav").wrap('<div class="nav-placeholder"></div>');
        $(".nav-placeholder").height($('nav').outerHeight());

        $(window).scroll(function() {
            var scrollPos = $(window).scrollTop();
            $(".value").html(scrollPos);
            if (scrollPos >= navOffset) {
                $("nav").addClass("fixed");
            } else {
                $("nav").removeClass("fixed");
            }
        });
    });
</script>
<div class="header">
    <p>this is header</p>
    <p>this is header</p>
    <p>this is header</p>
    <p>this is header</p>
    <p>this is header</p>
</div>
<nav>
    <ul>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Four</li>
    </ul>
    <div class="clearer"></div>
</nav>
<div class="value"></div>
<div class="dummy-content">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>

Thanks!

Upvotes: 1

Views: 534

Answers (2)

Moneer Kamal
Moneer Kamal

Reputation: 1877

if you want you can hide the header and then in the callback function add the fixed class or remove it something like this

$(document).ready(function() {
        var navOffset = $("nav").offset().left;

        $("nav").wrap('<div class="nav-placeholder"></div>');
        $(".nav-placeholder").height($('nav').outerHeight());

        $(window).scroll(function() {
            var scrollPos = $(window).scrollTop();
            $(".value").html(scrollPos);
            if (scrollPos >= navOffset) {

                $('.header').slideUp(500,function(){
                    $("nav").addClass("fixed");
                });

            } else {
                $('.header').slideDown(500,function(){
                    $("nav").removeClass("fixed");
                });
            }
        });
    });

i hope it helps

Upvotes: 0

Rafael Almeida
Rafael Almeida

Reputation: 687

You are getting the left offset using this:

var navOffset = $("nav").offset().left;

I think you want the top value:

var navOffset = $("nav").offset().top;

Updated Fiddle: http://jsfiddle.net/577br99o/8/

Upvotes: 1

Related Questions