【问题标题】:How to animate underline from left to right?如何从左到右为下划线设置动画?
【发布时间】:2021-06-17 16:25:54
【问题描述】:

我正在尝试从 uber.design 网站复制这种转变:

问题是我被困在扭转过渡:

.un {
  display: inline-block;
}

.un:after {
  content: '';
  width: 0px;
  height: 2px;
  display: block;
  background: black;
  transition: 300ms;
}

.un:hover:after {
  width: 100%;
<span class="un">Underlined Text</span>

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以使用渐变和延迟调整background-position以获得这样的效果:

    .un {
      display: inline-block;
      padding-bottom:2px;
      background-image: linear-gradient(#000, #000);
      background-position: 0 100%; /*OR bottom left*/
      background-size: 0% 2px;
      background-repeat: no-repeat;
      transition:
        background-size 0.3s,
        background-position 0s 0.3s; /*change after the size immediately*/
    }
    
    .un:hover {
      background-position: 100% 100%; /*OR bottom right*/
      background-size: 100% 2px;
    }
    <span class="un">Underlined Text</span>

    如果您想要悬停时的连续动画,您可以试试这个:

    .un {
      display: inline-block;
      padding-bottom:2px;
      background-image: linear-gradient(#000, #000);
      background-position: right -100% bottom 0;
      background-size: 200% 2px;
      background-repeat: no-repeat;
    }
    
    .un:hover {
      background-position: left -100% bottom 0;
      transition: background-position 0.5s;
    }
    <span class="un">Underlined Text</span>

    您可以查看此答案以获取有关如何完成不同值的计算的更多详细信息:Using percentage values with background-position on a linear-gradient


    另一种动画

    .un {
      display: inline-block;
      padding-bottom:2px;
      background-image: linear-gradient(to right, #000 33%,transparent 33% 66%,#000 66%);
      background-position: right bottom;
      background-size: 300% 2px;
      background-repeat: no-repeat;
    }
    
    .un:hover {
      background-position: left bottom;
      transition: background-position 0.5s;
    }
    <span class="un">Underlined Text</span>

    让我们不要忘记基本的:

    .un {
      display: inline-block;
      padding-bottom:2px;
      background-image: linear-gradient(#000,#000);
      background-position: right bottom; /* OR left bottom*/
      background-size: 100% 2px;
      background-repeat: no-repeat;
      transition: background-size 0.5s;
    }
    
    .un:hover {
      background-size: 0% 2px;
    }
    <span class="un">Underlined Text</span>

    您可以在这里找到更多技术:https://dev.to/afif/100-underline-overlay-animation-the-ultimate-css-collection-4p40

    【讨论】:

      【解决方案2】:

      您需要将伪元素进行绝对定位并使用:not 选择器来重现此效果。

      .un {
        display: inline-block;
        position: relative;
      }
      
      .un:after {
        content: '';
        width: 0px;
        height: 2px;
        position: absolute;
        top: 100%;
        left: 0;
        background: black;
        transition: 300ms;
      }
      
      .un:hover:after {
        width: 100%;
      }
      
      .un:not(:hover):after {
        right: 0;
        left: auto;
      }
      <span class="un">Underlined Text</span>

      【讨论】:

      • not(hover) 部分中的left: auto 是做什么的?
      • 你将如何完成这个 css,以便在点击时运行(例如在移动设备上的位置)?
      • 附加事件点击并使用类而不是悬停选择器。
      【解决方案3】:

      没有 :not 选择器或渐变,最简单的解决方案是在左右位置之间切换,例如在代码中。

      span.un {
        position: relative;
      }
      
      span.un::after {
        position: absolute;
        content: "";
        background: black;
        bottom: 0;
        right: 0;
        height: 2px;
        width: 0%;
        transition: 300ms ease-in-out;
      }
      
      span.un:hover::after {
        width: 100%;
        left: 0;
      }
      <span class="un">Underline me</span>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 2019-07-03
        • 2017-05-08
        • 1970-01-01
        相关资源
        最近更新 更多