首页 > 解决方案 > 固定 div 但在页脚之前停止

问题描述

我需要固定 div(侧边栏),但必须在页脚之前停止。我写了一个代码,我已经写了一个星期了,我做不到->pastebin.com/q85pPBpc

标签: javascriptphphtmlcss

解决方案


有一个非常短的 CSS 解决方案。它来自 Bootstrap 4。我想这就是你所需要的。

您的网站应具有以下结构:

<header></header>
<main>
    <aside class="sticky-top"></aside>

    <section></section>
    <section></section>
    <section></section>
</main>
<footer></footer>

简单的 CSS 会让你的侧边栏变得aside粘稠,它会留在主要部分,不会进入页脚。

@supports ((position: -webkit-sticky) or (position: sticky)) {
   .sticky-top {
       position: -webkit-sticky;
       position: sticky;
       top: 0;
       z-index: 1020;
   }
}

推荐阅读