【问题标题】:Make vertical scrollbar in bootstrap navbar when there are many items当有很多项目时,在引导导航栏中制作垂直滚动条
【发布时间】:2020-05-22 08:13:55
【问题描述】:

我在 react 应用程序中使用引导程序制作了一个侧边栏。代码如下:

import React from 'react';
import './style_sidebar.css';

export default class Sidebar2 extends React.Component {
    render() {
        return (
            <div>
                <ul className="nav flex-column sidebar bg-dark">
                    <h3 className="text-warning mt-3 ml-2">Bootstrap Sidebar</h3>
                    <i className="fa fa-user-circle text-white mt-3 ml-5 pl-4 icon-user" style={{'fontSize': '5em'}}></i>
                    <h4 className="text-warning ml-3 mt-5">Components</h4>
                    <li className="nav-item">
                        <a className="nav-link text-light" href="#a"><i className="fa fa-table fa-lg"></i> Table<hr className="bg-warning" /></a>
                    </li>
                    <li className="nav-item">
                        <a className="nav-link text-light" href="#b"><i className="fa fa-bar-chart fa-lg"></i> Chart<hr className="bg-warning" /></a>
                    </li>
                    <li className="nav-item">
                        <a className="nav-link text-light" href="#c"><i className="fa fa-wpforms fa-lg"></i> Form Elements<hr className="bg-warning" /></a>
                    </li>
                    <li className="nav-item">
                        <a className="nav-link text-light" href="#c"><i className="fa fa-lemon-o fa-lg"></i> Coming Soon<hr className="bg-warning" /></a>
                    </li>
                    <li className="nav-item">
                        <a className="nav-link text-light" href="#c"><i className="fa fa-lemon-o fa-lg"></i> Coming Soon<hr className="bg-warning" /></a>
                    </li>
                </ul>
            </div>
        );
    }
}

而css部分是:

.sidebar {
    width: max-content;
    height: 100vh;
    padding: 10px;
    position: fixed;
    z-index: -1;
}

.user-icon {
    border: 1px solid;
    border-radius: 40px;
}

/* @media (max-width: 767px) {
    .sidebar {
      width: 100%;
      height: max-content;
    }
} */

@media only screen and (max-width: 600px) {
    .sidebar {
        width: 100%;
        height: max-content;
    }

    h3 {
        align-self: center;
    }  

    .icon-user {
        align-self: center;
        margin-right: 100px;
    }
  }

现在,当我向ul 添加更多项目时,这些项目将被添加到页面顶部。因此,当项目数量更多时,我使用overflow-y:scroll 制作垂直滚动条。但不是y-axis 滚动条,而是水平滚动条。有任何解决这个问题的方法吗?

我也无法使其响应。我尝试使用media query,但由于我是引导程序新手,我发现它很难做到。

还有如何为侧边栏制作切换按钮,当屏幕尺寸较小时,侧边栏应该折叠。我想你明白我想说什么。我正在使用最新版本的 Bootstrap。怎么做?

【问题讨论】:

    标签: css twitter-bootstrap bootstrap-4 navbar react-bootstrap


    【解决方案1】:

    你可以用这个 flex-wrap 属性解决滚动问题:

    .sidebar {
    width: max-content;
    height: 100vh;
    padding: 10px;
    position: fixed;
    z-index: -1;
    -ms-flex-wrap: nowrap;
    flex-wrap: nowrap;
    overflow-y: auto;
    

    }

    为了响应,最初隐藏侧边栏并在单击切换按钮时切换它。

    【讨论】:

    • 它不起作用,我仍然得到水平滚动条而不是垂直滚动条。
    • 我已经解决了桌面的滚动问题,看看吧:codesandbox.io/s/charming-hill-34zev.
    • 我有 min-height: 100vh,将其更改为 height: 100vh & 它按预期工作。如果没有为定位的项目明确设置高度,则不会出现滚动。干得好@sakpal
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2018-03-24
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多