画线动画——SVG版和纯CSS版

概述

我们常常在网站中看到一些画线的动画效果,非常炫酷,大多数这种画线动画效果是通过SVG实现的,也有不少是用纯css实现的,下面我总结了一下这2种方法,供以后开发时参考,相信对其他人也有用。

参考资料:
移动端网页
PC端网页

SVG实现画线效果

在svg里面,可以用stroke-dashoffset属性来实现画线效果,示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>svg</title>
  <style>
  path{
      stroke-dasharray: 1000;
      stroke-dashoffset: 1000;
      animation: draw 5s ease 3;
  }
  @keyframes draw{
      0%{
          stroke-dashoffset: 1000;
      }
      100%{
          stroke-dashoffset: 0;
      }
  }
  </style>
</head>
<body>
<svg width="400px" height="400px" viewBox="100 250 400 400" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <path d="M153 334
  C153 334 151 334 151 334
  C151 339 153 344 156 344
  C164 344 171 339 171 334
  C171 322 164 314 156 314
  C142 314 131 322 131 334
  C131 350 142 364 156 364
  C175 364 191 350 191 334
  C191 311 175 294 156 294
  C131 294 111 311 111 334
  C111 361 131 384 156 384
  C186 384 211 361 211 334
  C211 300 186 274 156 274"
  style="fill:white;stroke:red;stroke-width:2"/>
</svg>
</body>
</html>

需要注意的是,必须要设置stroke-dasharray属性,并且和stroke-dashoffset一样长;其中1000是个自定义的值,只要比画线的总长度长就行了。

纯css画线

对于长方形或者正方形这些规则的图形,用css的transition效果即可,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>svg</title>
  <style>
    #border {
      position: relative;
      width: 300px;
      height: 300px;
    }
    .border-top {
      position: absolute;
      top: 50px;
      left: 50px;
      width: 0;
      height: 8px;
      background-color: #333;
      transition: all .5s 1s;
    }
    .border-bottom {
      position: absolute;
      bottom: 50px;
      right: 50px;
      width: 0;
      height: 8px;
      background-color: #333;
      transition: all .5s 2s;
    }
    .border-right {
      position: absolute;
      top: 50px;
      left: 250px;
      width: 8px;
      height: 0;
      background-color: #333;
      transition: all .5s 1.5s;
    }
    .border-left {
      position: absolute;
      bottom: 50px;
      left: 50px;
      width: 8px;
      height: 0;
      background-color: #333;
      transition: all .5s 2.5s;
    }
    .animate .border-top, .animate .border-bottom {
      width: 200px;
    }
    .animate .border-right, .animate .border-left {
      height: 200px;
    }
  </style>
</head>
<body>
  <div id="border">
    <i class="border-top"></i>
    <i class="border-right"></i>
    <i class="border-bottom"></i>
    <i class="border-left"></i>
  </div>
  <script>
    setTimeout(function() {
      document.getElementById('border').className += ' animate';
    }, 500);
  </script>
</body>
</html>

转载于:https://www.cnblogs.com/yangzhou33/p/9357537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值