css 一个文字悬停下划线效果?

作者站长头像
站长
· 阅读数 15

效果网址 : https://dzone.com

css 一个文字悬停下划线效果?

我看了一下, 这个效果感觉挺简单的, 但是自己试了以下感觉还是挺难的他们网页用到了 js, 不过我是用 css 实现的我参照着自己做的代码如下

<span>这是一段测试文字</span>

<style>
  span {
    display: inline-block;
  }

  span {
    transition: 3s;
    background: linear-gradient(0deg, black, black) no-repeat right bottom / 0 2px;
    background-size: 0 1px;
  }

  span:hover {
    background-position-x: left;
    background-size: 100% 1px;
  }
</style>

但是效果还有有些诧异, 为了更好看出效果我把过渡时间调整3秒

css 一个文字悬停下划线效果?

有没有大佬有好一点的实现方案? 用css或者js都可以

回复
1个回答
avatar
test
2024-06-20
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xiahuaxian</title>
    <style>
        .title {
            color: #333;
            line-height: 2;
            position: relative;
            display: inline-block;
        }
        .title span {
            background: linear-gradient(to right, #7e2f2b, #146420) no-repeat right bottom;
            background-size: 0% 2px;
            transition: background-size 1s ease;
        }
        .title:hover span {
            background: linear-gradient(to right, #7e2f2b, #146420) no-repeat left bottom;
            background-size: 100% 2px;
        }
    </style>
</head>
<body>

    <h2 class="title">
        <span>文字移入一段文字中下划线从左往右变长,然后鼠标移出又从左往右退出的效果,文字移入一段文字中下划线从左往右变长,然后鼠标移出又从左往右退出的效果,文字移入一段文字中下划线从左往右变长,然后鼠标移出又从左往右退出的效果,文字移入一段文字中下划线从左往右变长,然后鼠标移出又从左往右退出的效果。</span>
    </h2>

</body>
</html>
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容