我是新手。我试图在我的标题中进行导航。我使用Bulma CSS:
"bulma": "^0.7.4",
,我像这样导入Bulma文件:
import 'bulma/css/bulma.css';
它适用于大多数CSS,但在汉堡菜单中不适用于菜单当我单击汉堡按钮
时,不会倒塌这是我的标题代码:
import React,{Component} from 'react'
import {NavLink} from 'react-router-dom';
import style from './style.css';
class Header extends Component{
render(){
return (
<React.Fragment>
<nav className="navbar is-fixed-top" role="navigation" aria-label="main navigation">
<div className="navbar-brand">
<a href="/" className="navbar-item" to="/">
<img src="" alt="" width={112} height={28}/>
</a>
<a role="button" className="navbar-burger burger " aria-label="menu" aria-expanded="false" data-target="mainNavbar">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="mainNavbar" className="navbar-menu">
<div className="navbar-start">
<NavLink exact activeClassName={style.active} to="/" className="navbar-item">
Home
</NavLink>
<NavLink activeClassName={style.active} to="/films" className="navbar-item">
Films
</NavLink>
</div>
</div>
</nav>
</React.Fragment>
);
}
}
export default Header;
数据目标
汉堡按钮上的属性没有触发菜单。请让我知道我在做什么错。谢谢
bulma没有内置的切换事件,您必须通过在" navbar-burger"one_answers" navbar-menu"中手动创建它来创建它,我在这里用React Hooks
实现了 const [isActive, setisActive] = React.useState(false);
<nav className="navbar" role="navigation" aria-label="main navigation">
<div className="navbar-brand">
<a
onClick={() => {
setisActive(!isActive);
}}
role="button"
className={`navbar-burger burger ${isActive ? "is-active" : ""}`}
aria-label="menu"
aria-expanded="false"
data-target="navbarBasicExample"
>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div
id="navbarBasicExample"
className={`navbar-menu ${isActive ? "is-active" : ""}`}
>
<div className="navbar-start">
<a className="navbar-item">Home</a>
<a className="navbar-item">Documentation</a>
</div>
</div>
</nav>
正如另一个答案中所述,要使布尔玛的汉堡菜单起作用。以下是我的做法。toggleBurgerMenu
中的密钥。另外,需要在每个navbar-item
的onClick
事件中指定toggleBurgerMenu
,以便单击菜单项后关闭汉堡菜单。
import React from 'react';
import { Link } from 'react-router-dom';
const Navigation = (props) => {
function toggleBurgerMenu() {
document.querySelector('.navbar-menu').classList.toggle('is-active');
}
return (
<nav className="navbar" role="navigation" aria-label="main navigation">
<div className="navbar-brand">
<Link to="/" className="navbar-item">React App</Link>
<a role="button" className="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasic"
onClick={toggleBurgerMenu}>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="navbarBasic" className="navbar-menu">
<div className="navbar-start">
<Link to="/about" className="navbar-item" onClick={toggleBurgerMenu}>About</Link>
<Link to="/contact" className="navbar-item" onClick={toggleBurgerMenu}>Contact</Link>
<Link to="/notes" className="navbar-item" onClick={toggleBurgerMenu}>Notes</Link>
</div>
</div>
</nav>
);
}
export default Navigation;
当您单击汉堡时,'navbar-menu'应该是'navbar-menu is-Active'。在相反的情况下,您还应删除" IS-Active"。