This is now possible with CSS only, see https://stackoverflow.com/a/53832799/1482443
In case anyone needs jQuery approach, below is the original answer as it was posted 8 years ago:
I know this is tagged html/css only, but you can't do that with css only. Easiest way will be using some jQuery.
var fixmeTop = $('.fixme').offset().top; // get initial position of the element
$(window).scroll(function() { // assign scroll event listener
var currentScroll = $(window).scrollTop(); // get current position
if (currentScroll >= fixmeTop) { // apply position: fixed if you
$('.fixme').css({ // scroll to that element or below it
position: 'fixed',
top: '0',
left: '0'
});
} else { // apply position: static
$('.fixme').css({ // if you scroll above it
position: 'static'
});
}
});
http://jsfiddle.net/5n5MA/2/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…