jQuery(document).ready(function() {
var abovePos = 50;
var customMax = 1600;
var zIdx = 100;
var $bnr = $('#block');
var $win = $(window);
var $doc = $(document);
$bnr.css('zIndex', zIdx);
var offset = $bnr.offset();
var pos = $bnr.position();
var maxPos = customMax - abovePos;
$win.scroll(function() {
var st = $doc.scrollTop();
var newPos = st - offset.top;
if (st > offset.top) {
if (st > maxPos) {
newPos = maxPos - offset.top;
}
$bnr.stop().animate({
top: pos.top + newPos + abovePos
});
} else {
$bnr.stop().animate({
top: pos.top
})
}
});
});
在线DEMO: http://output.jsbin.com/mogufewodu