我试图了解伪元素,但似乎无法使用简单的按钮动画来处理它。
悬停时,我希望面板从下到上。类似于this page上的按钮(第1行,第2个按钮)。
根据我的理解,使用.btn:after
会在每个.btn
课程后添加任何css 。但那为什么这不起作用呢?
.btn {
border: 1px solid #65bb39;
color: #fff;
background-color: #65bb39;
cursor: pointer;
padding: 25px 80px;
display: inline-block;
margin: 15px 30px;
text-transform: uppercase;
outline: none;
position: relative;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
.btn:after {
content: '';
position: absolute;
z-index: -1;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
border: 1px solid #65bb39;
background-color: #fff;
}
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

<div class="text">
<span class="btn">Test</span>
</div>
&#13;
悬停时,我希望边框转为#65bb39
,挡块填充白色(从下到上)。
当然,对before
和after
的任何反馈表示赞赏!
答案 0 :(得分:1)
因此需要在btn中添加悬停状态:之后然后在两个状态之间转换css。请参阅下面的代码段。
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.btn {
color: #fff;
cursor: pointer;
padding: 25px 80px;
display: inline-block;
margin: 15px 30px;
text-transform: uppercase;
position: relative;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
.btn:hover{
color: #65bb39;
}
.btn:after, .btn:before {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0;
color: #65bb39;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
border: 1px solid #65bb39;
background-color: #fff;
}
.btn:before {
z-index: -2;
background-color: #65bb39;
height: 100%;
}
.btn:after {
z-index: -1;
}
.btn:hover:after {
height: 100%;
}
<div class="btn">Test</div>
答案 1 :(得分:1)
我认为这就是你要找的东西:
body,
html {
height: 100%;
width: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
a {
color: white;
text-decoration: none;
font-weight: bold;
font-size: 1.25em;
text-align: center;
line-height: 75px;
}
.button {
height: 75px;
width: 200px;
position: absolute;
border: 1px solid #65bb39;
background: transparent;
transition: all .3s ease-in-out;
}
.button::after {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #65bb39;
z-index: -1;
transition: all .3s ease-in-out;
}
.button:hover {
color: #65bb39;
}
.button:hover::after {
height: 0%;
}
.button:hover::before {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: white;
z-index: -2;
}
<a href="#" class="button">Button</a>
答案 2 :(得分:0)
你去了:
.btn {
border: 3px solid #65bb39;
background: none;
color: #65bb39;
cursor: pointer;
padding: 25px 80px;
display: inline-block;
margin: 15px 30px;
text-transform: uppercase;
outline: none;
position: relative;
transition: all 0.3s;
}
.btn:hover {
color: #FFFFFF;
}
.btn:after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 0;
width: 100%;
background: #65bb39;
z-index: -1;
transition: all 0.3s;
}
.btn:hover:after {
height: 100%;
}
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div class="text">
<span class="btn">Test</span>
</div>
答案 3 :(得分:-1)
试试这个
.btn {
border: 2px solid #65bb39;
background: none;
color: #fff;
cursor: pointer;
padding: 25px 80px;
display: inline-block;
margin: 15px 30px;
text-transform: uppercase;
outline: none;
position: relative;
transition: all 0.3s;
}
.btn:hover {
color: #65bb39;
}
.btn:after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #65bb39;
z-index: -1;
transition: all 0.4s;
}
.btn:hover:after {
height: 0%;
}
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
&#13;
<div class="text"><span class="btn">Test</span></div>
&#13;