Sticky CSS 粘性定位
<code readonly="true"> <xmp>.sticky { position: sticky; top: 0; background-color: #0ebac5; color: white; text-transform: capitalize; text-align: center; font-size: 1.2rem; display: grid; place-content: center; } </xmp> </code> </pre> <p>这是用于拉长页面高度的示例内容 上述方法仅在父容器具有足够滚动高度时才有效 . , , , , , , , , , , , , , , , 如果您希望在容器内部实现粘性定位但应用于整个页面,则应使用 JavaScript。 <pre data-line=""> <code readonly="true"> <xmp><style> body { height: 2000px; / Just for demonstration / } .fixed { position: relative; / Initially relative position / top: 0; / To keep the div at the top / width: 200px; / Adjust width as needed / height: 100px; / Adjust height as needed / background-color: lightblue; border: 1px solid blue; } .fixed.fixed-position { position: fixed; / Apply fixed position when scrolled / } </style> Fixed Div </xmp> </code> </pre> <style> .fixed { position: relative; / Initially relative position / bottom: 0; / To keep the div at the top / width: 200px; / Adjust width as needed / height: 100px; / Adjust height as needed / background-color: red; border: 1px solid blue; } .fixed.fixed-position { position: fixed; / Apply fixed position when scrolled / } </style> 固定定位 Div