将鼠标悬停以展开导航,而不是单击触发器?

时间:2019-02-11 16:36:34

标签: javascript html css reactjs react-bootstrap

我在页面的侧面有一个导航栏,我从网站上获取了代码。当前,它在顶部有一个按钮,当用户单击它时,它会展开。但是,我希望它在用户将鼠标悬停在它上面时扩展。并在鼠标离开时展开。但是,它是使用react-bootstrap编码的,所以我不确定如何编辑它。有什么想法吗?

导航栏:

<div className="App container">
      <SideNav
        onSelect={(selected) => {
            // Add your code here
        }}
      >
        <SideNav.Toggle  />
        <SideNav.Nav  defaultSelected="home">
            <NavItem eventKey="home">
                <NavIcon>
                    <Link to="/"><img src={Dash}/></Link>
                </NavIcon>
                <NavText>
                    <Link to="/">Dashboard</Link>
                </NavText>
            </NavItem>
            <NavItem eventKey="sites">
                <NavIcon>
                  <Link to="/sites"><img src={Site} /></Link>
                </NavIcon>
                <NavText>
                    <Link to="/sites">Sites</Link>
                </NavText>
            </NavItem>
            <NavItem eventKey="tours">
              <NavIcon>
                <Link to="/tours"><img src={Tour}/></Link>
              </NavIcon>
              <NavText>
                  <Link to="/tours">Tours</Link>
              </NavText>
            </NavItem>
            <NavItem eventKey="media">
                <NavIcon>
                  <Link to="/media"><img src={Media}/> </Link>
                </NavIcon>
                <NavText>
                    <Link to="/media">Media</Link>
                </NavText>
            </NavItem>
            <NavItem eventKey="newSite">
                <NavIcon>
                    <Link to="/newSite/details"><img src={NewSite} /></Link>
                </NavIcon>
                <NavText>
                    <Link to="/newSite/details">Add new Site</Link>
                </NavText>
            </NavItem>
            <NavItem eventKey="profile">
                <NavIcon>
                    <Link to="/profile"><img src={Profile} /></Link>
                </NavIcon>
                <NavText>
                    <Link to="/profile">Profile</Link>
                </NavText>
            </NavItem>

        </SideNav.Nav>
      </SideNav>
      <Routes />
      </div>

1 个答案:

答案 0 :(得分:0)

我个人认为这是一个非常糟糕,无法访问的UX设计,但是如果要继续进行下去,则需要实现onMouseEnter处理程序,如下所示:

  handleOnMouseEnter(path) {
    const { history } = this.props;

    console.log("handleOnMouseEnter: ", path);

    history.push(path);
  }

为了访问props中的history对象,您需要将组件包装在withRouter HOC中,如下所示:

const WrappedApp = withRouter(App);

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Router>
    <WrappedApp />
  </Router>,
  rootElement
);

然后您的NavItem看起来像这样:

    <NavItem
      eventKey="sites"
      onMouseEnter={() => this.handleOnMouseEnter("/sites")}
    >
      <NavText>
        <Link to="/sites">Sites</Link>
      </NavText>
    </NavItem>

有关有效的实现,请参见this CodeSandbox