滚动时保持 DIV 位于屏幕底部

QHu*_*_IT 1 html css

我在底部页脚有一个 DIV,我希望始终保持在屏幕的顶部和底部,但仅在顶部并在位置超过像素时固定在底部(例如:从上到下 500px)。

如果 < 500px 是正常 DIV,> 500px 则固定在底部。

我已经使用CSS,但这总是底部。

body {
    margin-bottom:90px;
}

#footer {
    bottom: 0px;
    position:fixed;
    margin-top: 10px;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

Aru*_*Aru 6

您需要为此使用媒体查询:

CSS:

#footer {
    top: 0px;  /* you can delete these two lines if you want the position as it is */
    position:fixed;
    margin-top: 10px;
    width: 100%;
}

@media screen and (min-width:500px){
    #footer { bottom: 0px; }
}
Run Code Online (Sandbox Code Playgroud)