我有一个引导导航,我添加了一个简单的CSS过渡。当用户徘徊在链接上时,它会正确擦拭并将背景颜色更改为蓝色。当用户在链接页面上时, .navactive
将添加到<li>
类中。这应该使背景颜色实心蓝色,因此用户知道他们在该页面上。线性梯度一直在克服这一点。
有办法
- 覆盖过渡并使其固体
- 禁用链接中与.navactive的链接过渡
谢谢
.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>
在这里发生了两件事:
-
.navactive
上的CSS特异性低于.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);
}
<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>
在侧节点上,您可能需要使用 font-style:italic
而不是skewx。