简体   繁体   English

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

[英]How to customize and change color of a menu in ant design?

I'm currently using reactJs along with ant design to help me build a website.我目前正在使用 reactJs 和 ant 设计来帮助我建立一个网站。 However, I've stumbled upon a problem.但是,我偶然发现了一个问题。 I can change the actual color of a menu, but I can't change the color of the menu item when it's been selected, or when the mouse is hovering over it.我可以更改菜单的实际颜色,但是当菜单项被选中或鼠标悬停在菜单项上时,我无法更改菜单项的颜色。 Here's the menu, Menu Image I want to change that white to a light green, but I haven't found any way to directly access it and change it.这是菜单, Menu Image我想把那个白色改成浅绿色,但我还没有找到任何直接访问和更改它的方法。 Also, I don't want to use LESS to do it.另外,我不想使用 LESS 来做到这一点。

Does anyone know how to fix this?有谁知道如何解决这一问题? Here's my code for the layout.这是我的布局代码。

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;

You need to change Link to NavLink and add attribute activeClassName您需要将 Link 更改为NavLink并添加属性activeClassName

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

For the hover options you can achieve it only with css对于 hover 选项,您只能使用 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 https://www.w3schools.com/cssref/sel_hover.asp

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

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