【问题标题】:How to make responsive hamburger menu with Bootstrap / React?如何使用 Bootstrap / React 制作响应式汉堡菜单?
【发布时间】:2021-08-04 05:08:50
【问题描述】:

编辑:我相信我的问题是加载 jquery 和 bootstrap JS 脚本。我如何在 React 中做到这一点?

我一直在尝试针对移动设备优化我的 React 网站,并且在移动设备上拥有一个可以转换为汉堡菜单式导航栏的导航栏。到目前为止,在Bootstrap Navbar Documentation 之后,我已经能够让我的导航栏删除项目并在缩小到移动尺寸时显示一个汉堡包图标,但是当我单击该图标时,什么都没有下降。

需要注意的一点:每次加载页面时,我都会使用道具将类传递给我的导航栏,这样我就可以使用一个组件为不同的页面设置各种导航栏样式。我将在我的 About.js(我的投资组合中的“关于我”页面的页面)中提供一个这样的调用,它与大多数其他调用相同。

导航栏.JS

// navigation bar class
class NavBar extends Component {
    render() {
        return (
            <nav id={this.props.navId} className={this.props.class}>
                <li id={this.props.logoId} className={this.props.logoClass}><p>{this.props.logoText}</p></li>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"><i className="fas fa-bars"></i></span>
                </button>
                <div className="collapse navbar-collapse">
                    <ul id="links" className="navbar-nav ml-auto">
                        <li className="nav-item active textlink"><Link className="nav-link" to="/home">Home</Link></li>
                        <li className="nav-item active textlink"><Link className="nav-link" to="/about">About Me</Link></li>
                        <li className="nav-item active textlink"><Link className="nav-link" to="/projects">Projects</Link></li>
                        <li className="nav-item active"><a className="nav-link" target="blank" href="https://github.com/QuintonPrice"><i className="fab fa-github"></i></a></li>
                        <li className="nav-item active"><a className="nav-link" target="blank" href="https://www.linkedin.com/in/quinton-price/"><i className="fab fa-linkedin"></i></a></li>
                    </ul>
                </div>
            </nav >
        );
    }
}

export default NavBar;

关于我的关于页面中导航栏组件的示例调用(这是我迄今为止用于所有其他页面但主页的相同调用)

<Navbar navId="navb" logoId="logo" logoText="qprice" logoClass="mr-auto" class="navbar bg-white navbar-expand-lg sticky-top navbar-fixed-top shadow ml-auto"/>

Navbar.css(注意:.navHome 类用于主页上显示的导航栏,.navb 用于我在其他所有页面上使用的导航栏样式):

/* nav bar */
nav {
    margin-bottom: 20px;
}

/* qprice logo */
#logo p {
    font-family: 'Raleway', sans-serif;
    font-weight: 700;
    color: #52B788;
    font-size: 2.3rem;
    margin-left: 15%;
    margin-right: 15%;
}

/* nav bar home links */
#navHome a {
    color: white;
    padding: 10px 15px;
    font-size: 1.5em;
    float: left;
    text-align: center;
    text-decoration: none;
    position: relative;
}

#navHome a:hover, #navb a:hover {
    color: #52B788;
}

/* nav bar home list items */
#navHome li {
    display: inline;
    margin-right: .5rem;
}

/* nav links */ 
#navb a {
    color: #343a40;
    padding: 15px;
    font-size: 1.5em;
    float: left;
    text-align: center;
    text-decoration: none;
    position: relative;
}

/* navigation list items */
#navb li {
    display: inline;
    margin-right: .5rem;
}

/* navbar link hover */
.textlink a:hover {
    color: #52B788;
}

.textlink a::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 1px;
    bottom: 0;
    left: 0;
    background-color: #52B788;
    visibility: hidden;
    transform: scaleX(0);
    transition: all .15s ease-in-out .05s;
  }

.textlink a:hover::before {
    visibility: visible;
    transform: scaleX(1);
  }

.fab {
    margin-right: .7rem;
    font-size: 1.5em;
    text-align: center;
    text-decoration: none;
  }

.fab:hover {
    color: #52B788;
    opacity: 0.7;
}

【问题讨论】:

  • 您的&lt;div className="collapse navbar-collapse"&gt; 缺少id="navbarTogglerDemo03" 以将其连接到导航栏切换器。
  • 添加ID后,还是遇到同样的问题
  • 好问题。这很奇怪。我的汉堡菜单在这里工作:jsbin.com/mufahar/1/edit?html,output。但是,虽然汉堡导航仍在我的 React 应用程序中呈现,但当我点击汉堡时,什么也没有发生:stackblitz.com/edit/react-kxakol?file=src/index.js。怎么回事?

标签: html css reactjs twitter-bootstrap bootstrap-4


【解决方案1】:

我昨天也遇到了同样的问题。我通过将脚本标签移动到 html 文件底部的结束正文标签并重新排列顺序来解决这个问题,以便 JQuery 脚本在 Bootstrap 脚本之前首先运行。

【讨论】:

  • 我试过这样做,但不幸的是没有雪茄。你是在使用 bootstrap-react 程序还是像往常一样使用 bootstrap?
  • 我没有将它与 react 一起使用,但我认为以正确的顺序调用脚本可能会有所帮助。抱歉,它不适合你。如果你给我一个回购链接,我可以进一步研究。
  • 这是我的仓库的链接:github.com/QuintonPrice/Portfolio
猜你喜欢
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多