Fixed Scroll

by Anton Kolesnikov

HTML

<header>
    Header is here
</header>
<div id="menu">
    Menu is here
</div>

CSS

body {
    height: 1000px;
}
header {
    height: 150px;
}
#menu {
    height: 50px;
    background-color: #ccc;
}
.fixed {
    position: fixed;
    top: 0;
    width: 100%;
}

JavaScript

var headerHeight = 150; // высота header

$(document).scroll( function() {
    var scrollTop = $(document).scrollTop();

    if (scrollTop > headerHeight) {
        $('#menu').addClass('fixed');
    } else if ($('#menu').hasClass('fixed')) {
        $('#menu').removeClass('fixed');
    }
});