做法是把 body 的捲動交給一個內層容器,讓固定在頂部的 div 不受捲動影響:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>頂部固定</title>
<style type="text/css">
html, body { overflow: hidden; margin: 0; width: 100%; height: 100%; }
.virtual_body { width: 100%; height: 100%; overflow-y: scroll; overflow-x: auto; }
.fixed_div {
position: absolute;
z-index: 2008;
top: 0; left: 0; right: 0;
height: 40px;
background: #CCCCCC;
border-bottom: 3px inset;
border-color: #006699;
}
</style>
</head>
<body>
<div class="fixed_div"><font color="red">鄭州大學</font></div>
<div class="virtual_body">
<div style="height:888px;">
<p>再見,我一定會回來看你的!</p>
</div>
</div>
</body>
</html>

原範例中註解掉的那幾行,就是早期的跨瀏覽器半透明方案:
filter: alpha(opacity=50); /* IE6-8 */
-moz-opacity: 0.5; /* 老 Firefox */
opacity: 0.5; /* 標準 */
今天只需要 opacity: 0.5,其餘兩行可以光榮退休。不過要注意 opacity 會連子元素一起變透明,文字也淡掉了。若你只想讓背景半透明、文字保持實心,該改用 rgba:
.fixed_div { background: rgba(204, 204, 204, 0.5); }
用 JS 假造一個捲動容器(virtual_body)是舊時代的技巧,因為 IE6 完全不支援 position: fixed。現代瀏覽器一行就解決:
.fixed_div {
position: fixed;
top: 0; left: 0; right: 0;
height: 40px;
z-index: 1000;
}
body { padding-top: 40px; } /* 避免內容被導覽列遮住 */
若還想要「毛玻璃」效果,現在有 backdrop-filter:
.fixed_div {
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
另外提醒兩點:導覽列固定後,記得用 padding-top 或 scroll-margin-top 補上高度,否則頁內錨點會被遮住;<font> 標籤早已廢棄,請改用 CSS 設定顏色。