我不认为只有 CSS 属性的解决方案。我会尝试使用position: absolute;
我的 CSS,并使用 JavaScript(我的示例需要 jQuery),例如:
jQuery(function($) { // document ready
var $win = $(window),
handler = function() {
// try not to overload browser, creating a throttle
var throttle,
throttleFn = function() {
// this is what happens on window resize
$('#header').css({
top: $win.scrollTop()
});
};
return function() {
clearTimeout(throttle);
throttle = setTimeout(throttleFn, 100);
};
};
$win.resize(handler());
});
它在移动设备中并不是很“酷”,但众所周知,Web 应用程序中的固定标头存在移动问题(与本机不同)。如果您需要,我可以使用 JSFiddle 示例进行更新。
查看演示:http: //jsfiddle.net/qaKT7/(您可以使用 100 值来获得更好的体验,也可以使用.animate()
而不是.css()
让它看起来更漂亮)