【发布时间】:2021-11-19 20:07:07
【问题描述】:
所以我有这个侧边栏,它有通往应用程序不同页面的路线。在悬停时,我将li 设置为带有右边框和相同颜色的图标。现在我一直在尝试将活动类添加到当前活动的路径中,其样式与 li (右边框和图标颜色)相同。但我做不到。
我已经尝试过post 的解决方案,但似乎没有任何效果。我尝试使用来自react-router-dom 的NavLink 并添加了activeClassName,但它不起作用。
这是代码的当前状态:
const Sidebar = () => {
return (
<div className={style.sidebarContainer} >
<li className={style.navItem} >
<Link to='/' activeClassName="active" >
<PersonIcon />
</Link>
</li>
<li className={style.navItem}>
<Link to='/example1' activeClassName="active" >
<SettingsIcon />
</Link>
</li>
<li className={style.navItem}>
<Link to='/example2' activeClassName="active" >
<LightModeIcon />
</Link>
</li>
</div>
)
}
这是样式表:
.sidebarContainer{
background: black;
}
.sidebarContainer a{
text-decoration: none;
color: gray;
width: 100%;
}
.navItem {
width: 100%;
padding: 1rem 0;
cursor: pointer;
border-right: 4px solid transparent;
transition: all 0.3s ease 0s !important;
list-style: none;
padding-left: 1.3rem;
display: flex;
align-items: center;
}
.navItem:hover {
border-right: 4px solid red;
}
.navItem:hover svg {
color: red;
}
.navItem svg {
font-size: 1.6rem !important;
transition: all 0.3s ease 0s !important;
}
它的外观如下:
【问题讨论】:
-
您是否遇到任何后端或前端错误?