我有一个按钮,但是当我将鼠标悬停在按钮上时,文本不想更改颜色(颜色更改类---- .active:hover
)。由于某些奇怪的原因,它不想更改。另外,正如您在我的代码中看到的那样,我想将文本颜色更改为#fff
(白色)。我经常使用:hover
选择器,但不知道问题出在哪里!
body {
font-family: "Exo 2", sans-serif;
margin: 0;
padding: 0;
justify-content: center;
height: 100vh;
align-items: center;
}
.active {
text-decoration: none;
color: #000;
position: relative;
padding: 8px 30px;
background: #fff;
text-transform: uppercase;
font-size: 15px;
box-sizing: border-box;
transition: 0.5s;
border: 2px solid #000;
}
.active:before {
content: '' ;
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 100%;
background: #000;
z-index: 1;
transform: scaleX(0);
transition: transform 0.5s;
transform-origin: left;
}
.active:hover {
color: #fff;
}
.active:hover:before {
transform: scaleX(1);
transition: transform 0.5s;
transform-origin: right;
}
.active:after {
content: '' ;
position: absolute;
top: 0;
right: 0;
width: 50%;
height: 100%;
background: #000;
z-index: 1;
transform: scaleX(0);
transition: transform 0.5s;
transform-origin: right;
}
.active:hover:after {
transform: scaleX(1);
transition: transform 0.5s;
transform-origin: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<a class="active" href="#">Login</a>
</body>
</html>
答案 0 :(得分:4)
您正在将:before
元素放置在具有该z-index
值的文本上。
根据您拥有的所有变体,还包括按钮的背景,我建议您对文本使用<span>
并在该元素上使用较高的z-index
:
body {
font-family: sans-serif;
margin: 30px;
text-align: center;
}
.active {
text-decoration: none;
color: #000;
position: relative;
padding: 8px 30px;
background: #fff;
font-size: 15px;
transition: 0.5s;
border: 2px solid #000;
}
.active span {
position: relative;
transition: color .3s linear;
z-index: 2
}
.active:before,
.active:after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 100%;
background: #000;
z-index: 1;
transform: scaleX(0);
transition: transform 0.5s;
transform-origin: left;
}
.active:after {
left: auto;
right: 0;
transform-origin: right;
}
.active:hover span {
color: #fff;
}
.active:hover:before,
.active:hover:after {
transform: scaleX(1);
transition: transform 0.5s;
}
<a class="active" href="#"><span>LOGIN</span></a>
答案 1 :(得分:2)
仅使用背景色即可实现相同目的,而不再需要伪元素
body {
font-family: "Exo 2", sans-serif;
}
.active {
text-decoration: none;
color: #000;
position: relative;
padding: 8px 30px;
display:inline-block;
margin:10px;
background-image:
linear-gradient(#fff,#fff),
linear-gradient(#000,#000);
background-size:100% 200%,0% 200%;
background-position:left 50% top -100%,50% 50%;
background-repeat:no-repeat;
text-transform: uppercase;
font-size: 15px;
box-sizing: border-box;
transition:background-size 0.5s,background-position 0s 0.5s;
border: 2px solid #000;
}
.active:hover {
color:#fff;
background-size:0% 200%,800% 200%;
background-position:50% 50%;
transition:background-size 1s,background-position 0s 1s;
}
<a class="active" href="#">Login</a>
也这样:
body {
font-family: "Exo 2", sans-serif;
}
.active {
text-decoration: none;
color: #000;
position: relative;
padding: 8px 30px;
display:inline-block;
margin:10px;
background-image:
linear-gradient(#000,#000),
linear-gradient(#000,#000);
background-size:0% 100%;
background-position:left,right;
background-repeat:no-repeat;
text-transform: uppercase;
font-size: 15px;
box-sizing: border-box;
transition:0.5s;
border: 2px solid #000;
}
.active:hover {
color:#fff;
background-size:50.5% 100%;
}
<a class="active" href="#">Login</a>
也喜欢这个:
body {
font-family: "Exo 2", sans-serif;
}
.active {
text-decoration: none;
color: #000;
position: relative;
padding: 8px 30px;
display:inline-block;
margin:10px;
background-image:
linear-gradient(#000,#000);
background-size:0% 100%;
background-position:center;
background-repeat:no-repeat;
text-transform: uppercase;
font-size: 15px;
box-sizing: border-box;
transition:0.5s;
border: 2px solid #000;
}
.active:hover {
color:#fff;
background-size:100% 100%;
}
<a class="active" href="#">Login</a>
此处有关值计算的更多详细信息:https://stackoverflow.com/a/51734530/8620333