使用下面的代码,我得到一个从左到右的悬停下划线效果。
.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>
当你不在悬停状态时,:after元素返回到左边的初始状态。是否有任何方法,:后去右,而不是左当你离开悬停?
你可以尝试用动画的宽度来代替右/左属性。
.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%;
}
<p>hello, link is <a href="#" class="underline">underline</a></p>
请看下面的示例:https://jsfiddle.net/1gyksyoa/
基于这个答案:扩展悬停的底部边框,你可以改变悬停的transform-origin
属性来实现"hover out"你正在寻找的效果。下面是一个例子:
.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>