Try the following:
JsFiddle works here
$(document).ready(function() {
$(window).scroll(function() {
var scrollVal = $(this).scrollTop();
if ( scrollVal > 150) {
$('#subnav').css({'position':'fixed','top' :'0px'});
} else {
$('#subnav').css({'position':'static','top':'auto'});
}
});
});
Note. If you have only one value, you can use else, but if you have several values, I suggest not to use else, because it creates a conflict, use else ifintead.
source
share