CSS动画实现文字逐渐展示效果

CSS动画实现文字逐渐展示效果

效果如下

动效

主要使用了下面的样式:
将文字的颜色设置成透明
背景绘制在前景文本内(剪切到前景文本)

	color: transparent;
    -webkit-background-clip: text;
    background-clip: text;

然后使用css的动画,控制文字的背景的位置和大小,来实现文字动起来的效果
背景的颜色写了两个渐变:
第一个渐变实现类似光标的效果,光标是从左到右依次移动的。因此第一个背景色大小固定,通过控制背景X方向位置,来实现光标从左到右的移动
第二个背景色,是控制整体文字从左到右依次出现的。因此这个背景色变化逻辑,是通过控制背景的size来实现从左到右逐渐显示文字的效果

background: -webkit-linear-gradient(left,
            #047C88 0%,
            rgb(255, 255, 255) 45%,
            rgb(255, 255, 255) 55%,
            #047C88 100%) no-repeat 100% center / 60px 60px, linear-gradient(to right, #047C88, #047C88) 0 0 / 100% 100%;
        background-repeat: no-repeat;
        -webkit-background-clip: text;
<!DOCTYPE html>
<html>

<head>
  <style>
    .text-container {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100vw;
      height: 100vh;
    }

    .inspire-info-box {
      position: relative;
    }

    .inspire-info-bg {
      background-color: rgba(58, 156, 136, 0.29);
      width: 100%;
      height: 100%;
      position: absolute;
      z-index: -1;

      top: 0;
      left: 0;
      animation: inspireInfoBg 0.7s ease;
    }

    .inspire-info {
      padding: 5px 20px;
      font-size: 40px;
      font-weight: 500;
      letter-spacing: 0px;
      line-height: 60px;

      color: transparent;
      background-repeat: no-repeat;
      -webkit-background-clip: text;
      background-clip: text;
      animation: inspireInfoTxtGreen 1.4s ease-out;
      animation-delay: 0.7s;
      animation-fill-mode: forwards;
    }

    @keyframes inspireInfoBg {
      0% {
        left: 0;
        transform: translate(-50%, 50%) scale(0);
      }

      100% {
        left: 0;
        transform: translate(0, 0) scale(1);
      }
    }

    @keyframes inspireInfoTxtGreen {
      0% {
        background: -webkit-linear-gradient(left,
            #047C88 0%,
            rgb(255, 255, 255) 45%,
            rgb(255, 255, 255) 55%,
            #047C88 100%) no-repeat -20% center / 60px 60px, linear-gradient(to right, #047C88, #047C88) 0 0/ 0 100%;
        background-repeat: no-repeat;
        -webkit-background-clip: text;
      }

      99% {
        background: -webkit-linear-gradient(left,
            #047C88 0%,
            rgb(255, 255, 255) 45%,
            rgb(255, 255, 255) 55%,
            #047C88 100%) no-repeat 100% center / 60px 60px, linear-gradient(to right, #047C88, #047C88) 0 0 / 100% 100%;
        background-repeat: no-repeat;
        -webkit-background-clip: text;
      }

      100% {
        background: #047C88;
        background-repeat: no-repeat;
        -webkit-background-clip: text;
      }

    }
  </style>
</head>

<body>
  <div class="text-container">
    <div class="inspire-info-box">
      <div class="inspire-info">
        提供免费早餐哦!
      </div>
      <div class="inspire-info-bg">
      </div>
    </div>
  </div>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值