,你可以嘗試這樣的事情
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」)具有相同的高度50像素可以設置所有$(」 #top-pag1,#top-pag2,#top-pag3,#top-pag4「)css top:-50px和margin-bottom:-50px所以你不必設置像-50px,-100px的css值
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 210
#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/
這是任何接近你想要什麼? http://jsfiddle.net/tKeYe/(不需要JS)... – gvee