侧栏列上的Bootstrap粘性顶部不起作用

时间:2019-04-05 15:28:08

标签: html css bootstrap-4 sticky

我正在尝试在右侧创建一个粘性侧栏。侧边栏菜单位于网格列内。我正在将sticky-top类用作shown in this question,但仍然无法使用。

这是代码...

<div class="container min-vh-100 overflow-hidden">
    <nav class="navbar navbar-light navbar-expand">
        <a class="navbar-brand" href="#">Brand</a>
        <ul class="navbar-nav">
            <li class="nav-item"><a href="#" class="nav-link">Home</a></li>
        </ul>
    </nav>
    <div class="row">
        <div class="col-sm-8 content pt-4">
            ...
        </div>
        <div class="col-sm-4">
            <div class="menu sticky-top p-3 bg-light">
                <h5 class="text-primary">Sticky menu</h5>
                <div class="nav flex-column">
                    <a href="#" class="nav-link pl-0">Menu 1</a>
                    <a href="#" class="nav-link pl-0">Menu 2</a>
                    <a href="#" class="nav-link pl-0">Menu 3</a>
                </div>
            </div>
        </div>
    </div>
</div>

Codeply:https://www.codeply.com/go/xwYPD1B1tk

menu div是我在用户向下滚动时要坚持的顶部。

1 个答案:

答案 0 :(得分:1)

我发现,如果sticky元素的任何父容器使用overflow:hidden,则sticky将不起作用。从容器中删除overflow-hidden类可以使sticky-top正常工作。

<div class="container min-vh-100 overflow-hidden">
    <nav class="navbar navbar-light navbar-expand">
        ..
    </nav>
    <div class="row">
        <div class="col-sm-8 content pt-4">
            ...
        </div>
        <div class="col-sm-4">
            <div class="menu sticky-top p-3 bg-light">
                <h5 class="text-primary">Sticky menu</h5>
                <div class="nav flex-column">
                    ...
                </div>
            </div>
        </div>
    </div>
</div>

https://www.codeply.com/go/9Nf6pOa7TN