你可以试试这样的
var toppag=$("#top-pag1,#top-pag2,#top-pag3,#top-pag4");
var pag=$("#pag1,#pag2,#pag3,#pag4");
toppag.css({position:"fixed",bottom:"0px"});
$(window).scroll(function () {
var scroll=$(this).scrollTop()+$(this).height();
pag.each(function(i,n){
if(scroll > $(this).offset().top) {
toppag.eq(i).css({'position':'relative'});
if(i>0){
toppag.eq(i).css({'top':i*-50});
}
}
if(scroll < ($(this).offset().top + toppag.eq(i).height())) {
toppag.eq(i).css({'position':'fixed',"top":""});
}
});
});
http://jsfiddle.net/vV3nP/34/
如果所有 $("#pag1,#pag2,#pag3,#pag4") 具有相同的高度 50px 你可以设置所有 $("#top-pag1,#top- pag2,#top-pag3,#top-pag4") css top:-50px 和 margin-bottom:-50px 所以你不必在 css 值中设置 -50px, -100px
var toppag=$("#top-pag1,#top-pag2,#top-pag3,#top-pag4");
var pag=$("#pag1,#pag2,#pag3,#pag4");
$(window).scroll(function () {
var scroll=$(this).scrollTop()+$(this).height();
pag.each(function(i,n){
if(scroll > $(this).offset().top) {
toppag.eq(i).css({'position':'relative',"top":"-50px","margin-bottom":"-50px"});
}
else if(scroll < ($(this).offset().top + toppag.eq(i).height())) {
toppag.eq(i).css({'position':'fixed',"top":"","margin-bottom":""});
}
});
});
和这样的CSS
#pag2{
background:#565656;
}
#pag3{
background:#000;
}
#pag4{
background:#FF6258;
}
#top-pag1, #top-pag2, #top-pag3, #top-pag4{
height:50px; position:fixed;bottom:0px;
}
#top-pag1{
background:#8AC14D;
width:250px;
}
#top-pag2{
background:#565656;
width:200px;
}
#top-pag3{
background:#000;
color:#FFF;
width:150px;
}
#top-pag4{
background:#FF6258;
width:50px;
}
固定 jsfiddle http://jsfiddle.net/vV3nP/37/