Radek Tarant
Radek Tarant

Reputation: 227

Fixed navigation on top after scroll

I have this problem that my navigation menu has a bad fixed position after I scroll the website. I want to scroll the website down so my navigation menu remains at the same position. I need this menu to remain fixed at the top after scroll.

After the first scroll, I need this menu to jump to the top and be available for next scroll down but remains fixed at the top. And if I scroll back to base position of the website, I need the menu to be back to its start position(under the logo as now).

My actual CSS settings:

z-index: 9999;
position: fixed;

Javascript setup:

$("document").ready(function($){
    var nav = $('#menu');

    $(window).scroll(function () {
        if ($(this).scrollTop() > 125) {
            nav.addClass("f-nav");
        } else {
            nav.removeClass("f-nav");
        }
    });
});

But it doesn't work. Always if I scroll down, the navigation menu doesn't jump to the top.

You can see my problem: here

Upvotes: 1

Views: 1786

Answers (2)

dazunE
dazunE

Reputation: 328

Set your initial styles to

#menu { text-align: center; height: 65px; width: 100%; z-index: 9999; position: relative; background-color: #0F1113; border-bottom-width: 4px; border-bottom-style: solid; border-bottom-color: #63842d; }

then give this styles to the scrolling class

.f-nav { position:fixed !important; top:0; -webkit-transition: height 0.3s // these transitions for give smooth scroll; -moz-transition: height 0.3s // these transitions for give smooth scroll; transition: height 0.3s // these transitions for give smooth scroll; }

Upvotes: 1

4dgaurav
4dgaurav

Reputation: 11506

Demo

jquery

var lastScroll = 0;
var scrollToStick = $("header").height();

$(window).on('scroll', function (evt) {
    var newScroll = $(window).scrollTop();
    $('header').toggleClass('fxdHdr', newScroll > lastScroll && newScroll > scrollToStick);
    lastScroll = newScroll;
});

css

body {
    margin: 0;
    padding: 0;
}
.fxdHdr {
    -webkit-transform: translateY(-60px);
    -moz-transform: translateY(-60px);
    -ms-transform: translateY(-60px);
    -o-transform: translateY(-60px);
    transform: translateY(-60px);
    box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.14), 0px 2px 4px rgba(0, 0, 0, 0.28);
}
header {
    height: 60px;
    background: #d3145a;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    -webkit-transition: -webkit-transform 500ms ease;
    -moz-transition: -moz-transform 500ms ease;
    -ms-transition: -ms-transform 500ms ease;
    -o-transition: -o-transformtransform 500ms ease;
    transition: transform 500ms ease;
    text-align:center;
    line-height:60px;
}

Upvotes: 0

Related Questions