我有一个引导程序导航,我已经添加了一个简单的CSS转换。当用户将鼠标悬停在链接上时,它会向右擦除并将背景颜色更改为蓝色。当用户在链接页面上.navactive
添加到<li>
类时。这应该使背景颜色为蓝色,以便用户知道它们在该页面上。线性渐变保持了这一点。
有没有办法
由于
.nav li {
/* Old browsers */
background: linear-gradient(to right, #0943A7 50%, #FFFFFF 50%);
background-size: 200% 100%;
background-position: right bottom;
margin-left: 10px;
transition: all 1s ease;
}
.nav li:hover{
background-position: left bottom;
}
.navactive {
background-color: #0943A7 !important;
overflow: hidden;
transform: skewX(-18deg);
-webkit-transform: skewX(-18deg);
-ms-transform: skewX(-18deg);
}
HTML:
<nav class="navbar navbar-expand-lg navbar-light bg-faded">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="true" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse show" id="navbarResponsive">
<ul class="nav justify-content-center mx-md-auto">
<li class="<?php echo ($page == "home" ? "nav-item navactive" : "nav-item")?>">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="<?php echo ($page == "page2" ? "nav-item navactive" : "nav-item")?>">
<a class="nav-link" href="#">Page 2</a>
</li>
</ul>
</div>
</div>
</nav>
答案 0 :(得分:0)
这里发生了两件事:
.navactive
上的specificity低于.nav li
background:~~~
是background-image
的简写,background-image
覆盖background-color
将.navactive
规则更改为.nav li.navactive
应该注意特殊性。
将background-image:none;
添加到.navactive
规则将确保显示background-color
。
.nav li {
/* Old browsers */
background: linear-gradient(to right, #0943A7 50%, #FFFFFF 50%);
background-size: 200% 100%;
background-position: right bottom;
margin-left: 10px;
transition: all 1s ease;
}
.nav li:hover{
background-position: left bottom;
}
.nav li.navactive {
background-color: #0943A7 !important;
background-image:none;
overflow: hidden;
transform: skewX(-18deg);
-webkit-transform: skewX(-18deg);
-ms-transform: skewX(-18deg);
}
&#13;
<nav class="navbar navbar-expand-lg navbar-light bg-faded">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="true" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse show" id="navbarResponsive">
<ul class="nav justify-content-center mx-md-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="page2 nav-item navactive" >
<a class="nav-link" href="#">Page 2</a>
</li>
</ul>
</div>
</div>
</nav>
&#13;
在侧节点上,您可能希望使用font-style:italic
而不是skewX。