悬停进出时从左到右下划线

R M*_*R M 9 css css3 transitions css-transitions

使用以下代码,我从左到右得到悬停下划线效果.

.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;
}
Run Code Online (Sandbox Code Playgroud)
<p>hello, link is <a href="#" class="underline">underline</a>
</p>
Run Code Online (Sandbox Code Playgroud)

当你没有悬停时,:after元素返回到左边的初始状态.是否有任何方式:当您离开悬停时,向右移动而不向左移动?

Vla*_*acu 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%;
}
Run Code Online (Sandbox Code Playgroud)
<p>hello, link is <a href="#" class="underline">underline</a></p>
Run Code Online (Sandbox Code Playgroud)

看到这个小提琴的工作示例:https://jsfiddle.net/1gyksyoa/


web*_*iki 5

基于此答案:扩展悬停时的底部边框,可以更改悬停时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%;
}
Run Code Online (Sandbox Code Playgroud)
Here is some dummy text <a href="#" class="expand">expand</a>
Run Code Online (Sandbox Code Playgroud)