可以爲需要固定的元素加上 { position: fixed } 這個 CSS 屬性,再通過 top left right bottom 屬性調整位置。
(1)html中的代碼:
<!--右側導航欄-->
<div class="navRight">
</div>
(2)css樣式中的代碼:
/*右側導航欄的樣式*/
.navRight{
width: 40px;
height: 100%;
float: right;
position: fixed; /*滾動時可以固定在屏幕上*/
top: 0;
right: 0;
background: rgb(38,38,38);
z-index: 100; /*在前100層上浮動*/
}