【发布时间】:2015-04-02 22:34:59
【问题描述】:
我有这个codepen,它在<a> 元素上使用:after 来创建漂亮的悬停效果。我分叉了一个不同的 codepen,以便我可以将其分解为易于管理、易于理解的部分,但我仍然不完全确定 :after 是如何在这里创建效果的。
我知道 :after 是一个 pseudo-class 伪元素,它在伪元素之前的元素之后放置另一个元素,但我无法理解这里发生了什么。
有人可以帮我理解这是如何工作的吗?以下是相关的 HTML 和 CSS:
HTML:
<div class="wrapper">
<a class="underline animate">Hover me</a>
</div>
CSS:
body {
background-color: #334D5C;
cursor: pointer;
font-size: 62.5%;
}
.wrapper {
margin-top: 70px;
text-align: center;
}
// basic styles for <a>
a.underline {
font-size: 5rem;
color: #E27A3F;
position: relative;
}
// hover style for text only
a,a:visited,a:hover,a:active{
transition:0.5s color ease;
text-decoration:none;
color: #45B29D;
position: relative;
}
a.underline:after {
left: 0;
}
// Animation styling
a.animate:after{
bottom: 0em;
height:5px;
height:0.35rem;
width:0;
background-color: #45B29D;
content: "";
transition:0.5s width ease;
-webkit-backface-visibility:hidden;
backface-visibility:hidden;
position:absolute;
}
a.animate:hover:after{
width:100%;
}
@import url(http://fonts.googleapis.com/css?family=Slabo+27px);
body {
background-color: #334D5C;
cursor: pointer;
font-size: 62.5%;
}
.wrapper {
margin-top: 70px;
text-align: center;
}
a.underline {
font-size: 5rem;
font-family: 'Slabo 27px';
color: #E27A3F;
position: relative;
}
a,
a:visited,
a:hover,
a:active {
transition: 0.5s color ease;
text-decoration: none;
color: #45B29D;
position: relative;
}
a.underline:after {
left: 0;
}
a.animate:after {
bottom: 0em;
height: 5px;
height: 0.35rem;
width: 0;
background-color: #45B29D;
content: "";
transition: 0.5s width ease;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
position: absolute;
}
a.animate:hover:after {
width: 100%;
}
/*
a.underline:after {
left:50%;
-webkit-transform:translateX(-50%);
transform:translateX(-50%);
}
*/
<div class="wrapper">
<a class="underline animate">Hover me</a>
</div>
【问题讨论】:
-
立即试用 codepen 链接。
-
::after是一个伪元素,而不是一个伪类。 -
已修复,谢谢。想详细说明这是如何工作的?
-
我会的,但是Jonas got there first.
标签: css hover css-animations pseudo-element