从右到左导航动画下划线

时间:2019-06-18 13:16:57

标签: css

我需要进行导航,在悬停线上从左到右显示,然后在悬停时返回到左。

我已经拥有它了,但是它从左到右移动,然后消失到右边,但是我需要它回到左边

helm install -f myvalues.yaml ./helm-chart
.underline {
  display: inline;
  position: relative;
  overflow: hidden;
}

.underline:after {
  content: "";
  position: absolute;
  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%;
}

所以我想要的结果是,下划线悬停时,下划线返回到左侧。

谢谢!

3 个答案:

答案 0 :(得分:2)

  

所以我想要的结果是,下划线悬停时,下划线返回到左侧。

只需在样式表中进行一次更改-从左开始而不是从右开始放置它。

.underline {
  display: inline;
  position: relative;
  overflow: hidden;
}

.underline:after {
  content: "";
  position: absolute;
  left: 0; /* was: 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; */ /* redundant now */
  /* right: auto; */ /* redundant now */
  width: 100%;
}
<a href="#" class="underline">underline</a>

答案 1 :(得分:0)

只需将transform-origin:100% 0更改为悬停元素,所以下划线就从右到左。如果您应用transform-origin:0 100%,则取消轮廓从左向右移动

.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:100% 0;
}
 <a href="#" class="expand">Underline</a>

答案 2 :(得分:0)

正如CSS神@TemaniAfif所指出的,只需摆脱两种状态下的所有right定义,而在默认状态下使用left: 0;,就可以了:

.underline {
  overflow: hidden;
  position: relative;
}

.underline:after {
  background: #000;
  bottom: -5px;
  content: "";
  height: 4px;
  left: 0;
  position: absolute;
  transition: width .3s ease-out;
  width: 0;
}

.underline:hover:after,
.underline:focus:after,
.underline:active:after {
  width: 100%;
}
<a href="#" class="underline">underline</a>