使用以下代码,我从左到右得到悬停下划线效果。
.underline {
display: inline;
position: relative;
overflow: hidden;
}
.underline:after {
content: "";
position: absolute;
z-index: -1;
left: 0;
right: 100%;
bottom: -5px;
background: #000;
height: 4px;
transition-property: left right;
transition-duration: 0.3s;
transition-timing-function: ease-out;
}
.underline:hover:after,
.underline:focus:after,
.underline:active:after {
right: 0;
}

<p>hello, link is <a href="#" class="underline">underline</a>
</p>
&#13;
当你没有悬停时,:after元素返回到左边的初始状态。当你离开悬停时,有没有办法:在右边之后而不是在左边?
答案 0 :(得分:12)
您可以尝试动画宽度而不是右/左属性。
.underline {
display: inline;
position: relative;
overflow: hidden;
}
.underline:after {
content: "";
position: absolute;
z-index: -1;
right: 0;
width: 0;
bottom: -5px;
background: #000;
height: 4px;
transition-property: width;
transition-duration: 0.3s;
transition-timing-function: ease-out;
}
.underline:hover:after,
.underline:focus:after,
.underline:active:after {
left: 0;
right: auto;
width: 100%;
}
&#13;
<p>hello, link is <a href="#" class="underline">underline</a></p>
&#13;
请参阅此小提琴,了解一个工作示例:https://jsfiddle.net/1gyksyoa/
答案 1 :(得分:4)
根据以下答案:Expand bottom border on hover您可以在悬停时更改transform-origin
属性以实现“悬停”
你正在寻找的效果。这是一个例子:
.expand{
position:relative;
text-decoration:none;
display:inline-block;
}
.expand:after {
display:block;
content: '';
border-bottom: solid 3px #000;
transform: scaleX(0);
transition: transform 250ms ease-in-out;
transform-origin:100% 50%
}
.expand:hover:after {
transform: scaleX(1);
transform-origin:0 50%;
}
Here is some dummy text <a href="#" class="expand">expand</a>