简体   繁体   English

导航菜单列表(子菜单)下拉列表在reactjs中不起作用

[英]Navigation menu list (sub-menu)dropdown isn't working in reactjs

I want to implement a navbar like component in reactjs. 我想在reactjs中实现一个类似于组件的导航栏。 but pesudo-classes(:hover) for css seem not to be supported by react(css-in-js). 但是反应(css-in-js)似乎不支持css的pesudo-classes(:hover)。 I try the another way out to solve the problem using li and state control. 我尝试用另一种方法来解决使用li和状态控制的问题。

Another problem for this solution is that i can't ripped off the unordered bullet list using display:inline-block or list-style:none because it make the sub-menu items to be disappear when moving my pointer down to them.I am using material-ui/core as UI library 这个解决方案的另一个问题是我无法使用display:inline-blocklist-style:none来破坏无序的子弹列表,因为当我将指针向下移动时,它会使子菜单项消失。我是使用material-ui / core作为UI库

An example of my code: 我的代码示例:

showMenu = () =>{
    this.setState({showMenu: true});
}

hideMenu = () =>{
    this.setState({showMenu: false});
}

html, css

dropdownMenu:{
    position: "absolute",
    fontSize: "0.9rem",
    marginTop: 0,
    right: 15,
    left: "auto",
    top: "63px",
    backgroundColor:"white", 
    border:"1px solid #D9E4E3", 
    boxShadow: "0px 3px 21px 0px rgba(0, 0, 0, 0.2)",
    animationDuration: "0.25s",
    animationFillMode: "both",
},

<div>
    <li onMouseLeave={this.hideMenu}>
        <div className={classes.sectionDesktop} onMouseEnter={this.showMenu}>
            <Avatar style={{backgroundColor: "transparent"}}><Icon>account_circle</Icon></Avatar>
            <span style={{fontWeight: 500, margin: "0px .5rem", lineHeight: "40px"}}>Louis Barnett</span>
        </div>
        {this.state.showMenu &&
            <div className={classes.dropdownMenu} >
                    <SideMenuItem label="My Profile" icon={this.renderIcon('person')} onClick={() => this.handleMenuClick("/me")}/>
                    <SideMenuItem label="Settings" icon={this.renderIcon('settings')} />
                    <SideMenuItem label="Logout" icon={this.renderIcon('exit_to_app')} onClick={this.handleSignoutClick}/>
            </div>
        }
    </li>
</div>

SideMenuItem Component SideMenuItem组件

render() {
    const selected = this.props.location.pathname === this.props.path;
    return(
    <ListItem button dense selected={selected} onClick={this.handleOnClick}>
        <ListItemIcon>
            {this.props.open || !this.props.label ? this.props.icon : <Tooltip title={this.props.label}>{this.props.icon}</Tooltip>}
        </ListItemIcon>
        <ListItemText primary={this.props.label}/>
    </ListItem>
    );
}

Hello mate the :hover styling should work in this case. 你好伙伴:悬停样式应该在这种情况下工作。 Just try specifying it in a style-sheet instead of inline style. 只需尝试在样式表中指定它而不是内联样式。

Sample code 示例代码

yourCssFile.css yourCssFile.css

.my-button {
   background-color: yellow;
 }

In your component 在您的组件中

import './path_to_css/yourCssFile.css';

In the button 在按钮中

<button class="my-button">Click me</button>

This will apply your styles for your button. 这将为您的按钮应用您的样式。 Similarly you can try out hover.Check out the reference ! 同样你可以尝试悬停。查看参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM