下面是一个模拟在 iOS 设备上看到的 Sticky header 效果的小脚本。
$('.scrolllist').scroll(function(){
$(this).find('ul').each(function(){
if($(this).position().top <= 0){
$(this).addClass('abs').find('strong').removeClass('mov');
if($(this).position().top <= ($(this).height() * -1)){
$(this).removeClass('abs');
$(this).find('strong').addClass('mov');
}
else {
$(this).addClass('abs');
$(this).find('strong').removeClass('mov');
}
}
else {
$(this).removeClass('abs').find('strong').removeClass('mov');
}
});
});
它通过将每个元素的状态从postion:absolute, top:0
to更改为position:absolute, bottom:0;
同时更改包含<ul>
from position:relative
to 来更改每个元素的位置position:static
示例:http: //jsfiddle.net/dMJqj/80/
有什么办法可以让它稍微平滑一点。它在 Chrome 和 Firefox 上看起来有点生涩,有时可能需要几分之一秒的时间才能触发,这很明显,因为粘性标题似乎闪烁。