如何在蚂蚁设计中自定义和更改菜单的颜色?

时间:2019-09-21 14:48:41

标签: javascript css reactjs menuitem antd

我目前正在使用reactJs和ant design来帮助我建立一个网站。但是,我偶然发现了一个问题。我可以更改菜单的实际颜色,但是在选择菜单项或将鼠标悬停在菜单项上时,不能更改其颜色。这里是菜单,Menu Image我想将白色更改为浅绿色,但是还没有找到直接访问和更改它的方法。另外,我不想用LESS来做。

有人知道如何解决此问题吗?这是我的布局代码。

import { Layout, Menu, Icon, Row, Button, Col } from 'antd';
import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Link } from "react-router-dom";


const { Header, Sider, Content } = Layout;
const { SubMenu } = Menu;

class BasicLayout extends React.Component {
  state = {
    collapsed: false,
    path: this.path
  };

  toggle = () => {
    this.setState({
      collapsed: !this.state.collapsed,
    });
  };

  render() {
    return (
      <Layout>
        <Sider className='ant-menu' trigger={null} collapsible collapsed={this.state.collapsed}>
          <div className="logo" />
          <Menu className='ant-menu' mode="inline" defaultSelectedKeys={['1']}>
            <Menu.Item key="1" >
              <Link to='/'>
                <Icon type="home" />
                <span>Home</span>
              </Link>
            </Menu.Item>
            <Menu.Item key="2">
              <Link to='/about'>
                <Icon type="plus" />
                <span>About</span>
              </Link>
            </Menu.Item>
            <SubMenu key="3" title={<span><Icon type="database" /><span>Directory</span></span>}>
                <Menu.Item>
                  <Link to='/resources/living'>
                    <span>Living Resources</span>
                  </Link>
                </Menu.Item>
                <Menu.Item>
                  <Link to='/water2'>
                    <span>Non-Living Resources</span>
                  </Link>
                </Menu.Item>
                <Menu.Item>
                  <Link to='/resources/recycle'>
                    <span>Recycling Resources</span>
                  </Link>
                </Menu.Item>

                <Menu.Item>
                  <Link to='/resources/reducing'>
                    <span>Reducing Resources</span>
                  </Link>
                </Menu.Item>
                <Menu.Item>
                  <Link to='/resources'>
                    <span>General</span>
                  </Link>
                </Menu.Item>
            </SubMenu>
          </Menu>
        </Sider>
        <Layout>
          <Header style={{ background: '#fff', padding: 0 }}>
            <Icon
              className="trigger"
              type={this.state.collapsed ? 'menu-unfold' : 'menu-fold'}
              onClick={this.toggle}
            />
          </Header>
          <Content
            style={{
              margin: '24px 16px',
              padding: 24,
              background: '#fff',
              minHeight: 280,
            }}
          >
            {this.props.children}
          </Content>
        </Layout>
      </Layout>
    );
  }
}
export default BasicLayout;

1 个答案:

答案 0 :(得分:0)

您需要将链接更改为 NavLink ,并添加属性 activeClassName

<NavLink  to="/portoflio" activeClassName="your-active-class" className="link">Portoflio</NavLink>

对于悬停选项,您只能使用css来实现

a.link:hover {
  /*Any style you want to have link on mouse over*/
  background-color: yellow;
}
.your-active-class{
 /*Any style you want to have link that is active*/
 background-color: yellow;
 }

https://www.w3schools.com/cssref/sel_hover.asp