【发布时间】:2020-02-08 08:34:51
【问题描述】:
我在导航栏中有一个按钮,当它被点击时,它应该在“活动”类中。导航栏开始时背景色透明,当单击按钮时,导航栏会变为背景色白色(不起作用)。当我在 .active 中使用 !important 时,它确实有效,但在单击按钮之前它是白色的(不透明)。这些样式在媒体查询中。有没有更好的方法来做到这一点?
html...
<nav class="navbar-default">
<div class="container-fluid no-transition">
<div value="colorChange" class="topNav">
<div class="navbar-header">
<button type="button" value="hideMenu" class="navbar-toggle button_container collapsed" [ngClass]="{'active': !menuCollapsed}" (click)="toggleMenu()">
<div>
<mat-icon class="menuIcon">menu</mat-icon>
</div>
</button>
</div>
</div>
</div>
</nav>
SCSS...
.topNav {
background-color: rgba(0, 0, 0, 0) !important;
}
// adding !important to below turns it white, but no transparent before click
.navbar-toggle.active, .topNav {
background-color: white;
}
【问题讨论】:
-
附带说明,您不应该将
div放在button中,因为它是无效的HTML。 -
感谢您的留言。如果您有任何建议,请告诉我(span 不起作用)。
-
为什么
span不起作用?这是我的建议,但您可能需要使用display: block;对其进行样式设置 -
点击时更改颜色应该使用 :active 伪类。不确定这是否会与任何其他应用的 css 冲突。但是基于上面的例子,删除 !important 并将你的 navbar-toggle 选择器更新为 .navbar-toggle:active, .topNav { background-color: yellow; } 应该会让您更进一步了解您正在寻找的东西。
-
@IanDevlin 是的,谢谢你, display: block 让 span 工作了。