【发布时间】:2019-07-16 21:05:57
【问题描述】:
我有一个单页反应应用程序(而不是所有内容都在页面上),我希望使用 react-router 滚动到同一页面上的必要组件。 这是我的导航代码,
class Navbar extends React.Component {
constructor(props){
super(props)
}
renderMain() {
return (
<div></div>
//return home
);
}
handleScroll = e => {
e.preventDefault();
const main = this.main.current;
window.scrollTo({
top: main.offsetTop,
left: 0,
behavior: "instant"
});
};
render(){
return (
<div className="navbar container">
<div className="logo">
<img src={logo}></img>
</div>
<BrowserRouter>
<div className="navigation">
<ul>
<li className="listPadding"><Link onClick={this.handleScroll}
to="/"
className="navLink"
activeClassName="activeRoute" />HOME</li>
<li className="listPadding"><Link onClick={this.handleScroll}
to="/whats-crcl" className="navLink"
activeClassName="activeRoute"/>WHAT'S CRCL?</li>
<li className="listPadding"><Link onClick={this.handleScroll}
to="/founders" className="navLink"
activeClassName="activeRoute"/>THE FOUNDERS</li>
<li className="listPadding"><Link onClick={this.handleScroll}
to="/careers" className="navLink"
activeClassName="activeRoute"/>WE'RE HIRING!</li>
<li className="button"><Link to=""/>READ THE BLOG</li>
</ul>
<Switch>
<Route exact path="/" component={() => this.renderMain()} />
<Route exact path="/whats-crcl" render={() => Snippets} />
<Route exact path="/the-founders" render={() => MainContent} />
<Route exact path="/whats-crcl" render={() => Careers} />
<Route render={() => <h1>Page not found</h1>} />
</Switch>
</div>
</BrowserRouter>
</div>
);
}
}
这是我的 CSS:
.navigation {
ul {
li {
font-family: 'Roboto', sans-serif;
font-size: 1.2em;
font-weight: 400;
color: #ffffff;
list-style: none;
display: inline-block;
a.navLink:link {
color: #fff;
font-size: 1.6rem;
line-height: 1.6rem;
text-decoration: none;
}
a.navLink:visited {
color: #fff;
}
a.navLink:hover {
color: #595959;
}
a.navLink:active {
color: #595959;
}
}
}
}
.activeRoute {
background-color: yellow;
border-bottom: 0.4rem solid teal;
cursor: not-allowed;
}
我是 React 新手。我的问题是:
- 导航元素也不会像普通的
a标签那样显示光标? - 这会在页面上弹出组件,如何防止该行为并向下滚动到同一页面上的相关组件?
【问题讨论】:
标签: javascript css reactjs react-router