css - 按下菜单切换时导航栏菜单不显示
问题描述
我的导航栏按照我想要的方式对齐,但是每当我按下菜单时,menu-toggle
我的菜单都不会显示。任何帮助,将不胜感激。
导航.jsx:
export class Nav extends Component {
state = {
toggle: false
}
menuToggle = () => {
this.setState({toggle: !this.state.toggle})
}
render() {
return (
<nav>
<ul>
<li><a><Link to="/" className="homeNav">Home</Link></a></li>
<li><a><Link to="/Converter" className="convertNav">Online Converter</Link></a></li>
<li><a><Link to="/ihertz" className="ihertzNav">432hz App</Link></a></li>
<li><a><Link to="/about" className="aboutNav">About Us</Link></a></li>
<li><a><Link to="/why432" className="whyNav">Why 432Hz?</Link></a></li>
<li><a><Link to="/contact" className="contactNav">Contact Us</Link></a></li>
<li className="close" onClick={this.menuToggle}>X</li>
</ul>
<div className="menu-toggle" onClick={this.menuToggle}><i className="fa fa-bars" aria-hidden="true"></i></div>
</nav>
)
}
}
导航.css:
.menu-toggle {
display: none;
color: #000;
left: 45%;
top: 20px;
position: absolute;
cursor: pointer;
line-height: 50px;
font-size: 24px;
}
@media (max-width: 728px) {
header ul {
position: fixed;
background: #1c72c9;
top: 0;
left: -100%;
width: 100%;
height: 100vh;
justify-content: center;
display: flex;
flex-direction: column;
align-items: center;
z-index: 99;
transition: 0.5 linear;
}
.menu-toggle {
display: block;
right: 30%;
}
header ul li {
line-height: normal;
}
.close {
display: block;
font-size: 20px;
}
header .headerLogo {
width: 85px;
}
}
解决方案
You're not actually doing anything when you update this.state.toggle
- you would need to change something in your render
function to respond to this change. For instance:
const navStyle = { display: this.state.toggle ? 'block' : 'none' }
...
<nav style={navStyle}>
Or, you can add something like .show
/ .hide
to your CSS:
.hide { display: none; }
.show { display: block; }
So you can use this in your HMTL:
let navClassName = this.state.toggle ? 'show' : 'hide';
...
<nav className={navClassName}>
推荐阅读
- ios - SDK 在 iOS 中通过 Objective-C 动态使用托管应用程序中的类
- python - 将布尔值插入mysql数据库时出现Python mysql.connector错误
- c# - 如何在 C# 中正确锁定对象以进行扩展方法调用
- python - 与列的标题进行比较并在 python 中检索其数据
- angular - 访问组件的实例并在角度 6 中更改其值
- dart - 需要帮助在颤振中访问断言捆绑文件
- c - 警告:格式“%p”需要“void *”类型的参数,但参数 3 的类型为“char **”
- clojure - 如何在 clojure 中调用'final G.Run run = g.new Run()'
- javascript - 如何仅更新数组中的单个值
- css - 自定义 CSS 被 bootstrap.css.map 覆盖?