[英]react navbar button , how to set active
我希望我的侧边栏按钮在访问其导航的页面时更改颜色。
I had this problem before and :active
didn't help that much because it returns to its original css once I click other part in my page. 我之前遇到过这个问题, :active
并没有太大帮助,因为一旦我单击页面的其他部分,它就会返回其原始CSS。
What you should do is create a class for your selected navbar button 您应该做的是为所选的导航栏按钮创建一个类
.current_active_navbar {
background-color: $main-red;
}
then in your js, create a condition that will apply the .current_active_navbar
to navbar based on your current URL. 然后在您的js中,创建一个条件,该条件将根据当前URL将.current_active_navbar
应用于导航栏。 You can get your current URL through this window.location.href
您可以通过此window.location.href
获取当前URL。
So let's say your current url is localhost:3000/timer
then you can have your condition like this 因此,假设您当前的网址是localhost:3000/timer
那么您可以像这样设置条件
if(window.location.href.split("/")[3] === "timer")
我不是用CSS自己太优秀了,但这个做了解释它的一个不错的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.