animate() , , , jQuery . , .
if.
.
, css.
$(window).scroll(function() {
var scrollVal = $(this).scrollTop();
if ( scrollVal > offset.top) {
$sidebar.css({
'margin-top': (($window.scrollTop() - offset.top) + topPadding) + 'px'
});
} else {
$sidebar.css({'margin-top':'0px'});
}
});
. if else if, else, . , .
UPDATE:
. , , html :
<div class="wrapper">
<div class="header">header</div>
<span id="subnav">hold this</span>
</div>
jQuery :
jsFiddle.
$(document).ready(function() {
$(window).scroll(function() {
var headerH = $('.header').outerHeight(true);
console.log(headerH);
var scrollVal = $(this).scrollTop();
if ( scrollVal > headerH ) {
$('#subnav').css({'position':'fixed','top' :'0px'});
} else {
$('#subnav').css({'position':'static','top':'0px'});
}
});
});