SVG学习笔记(二)做一个Loading动画

该博客介绍了用SVG制作Loading动画的方法。此前加载效果常用HTML结合CSS,此次用SVG更省事。制作前需了解stroke - dasharray和stroke - dashoffset属性,实现了基础、加速、滚动、心电图四种版本的动画,结合CSS3动画可快速制作美观动效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用SVG做一个Loading动画

博客原文链接

概述

之前做加载效果都是用的html结合css,这次用SVG感觉还挺省事的

准备

开始做这个加载效果之前,需要先了解一下两个属性,stroke-dasharray和stroke-dashoffset

这两个属性可以看下面张鑫旭大神的文章,说的还是很清楚的

stroke-dasharray和stroke-dashoffset资料

实现

1、基础效果

代码:

<svg width="100" height="100">
  <style>
    #svg-loading-base circle {
      stroke-dasharray: 250;
      stroke-dashoffset: 250;
      animation: svg-loading-base 1s ease infinite;
    }
    @keyframes svg-loading-base {
      to {
        stroke-dashoffset: 0;
        opacity: 0.1;
      }
    }
  </style>
  <g id="svg-loading-base">
    <text x="50" y="55" style="text-anchor: middle; fill: #5CC9F5; font-size: 14px;">加载中</text>
    <circle cx="50" cy="50" r="40" style="fill: none; stroke: #5CC9F5; stroke-width: 5px; stroke-dasharray: null; stroke-dashoffset: null" />
  </g>
</svg>
复制代码

这个基础版比较简单,就是单纯通过stroke-dasharray和stroke-dashoffset两个属性来设置线段分隔长度和偏移量,然后使用animation动画逐渐减少stroke-dashoffset偏移量的值实现

2、加速版

代码:

<svg width="100" height="100">
  <style>
    #svg-loading-fast circle {
      animation: svg-loading-fast 2s ease infinite;
    }
    @keyframes svg-loading-fast {
      from {
        stroke-dasharray: 250;
        stroke-dashoffset: 250;
      }
      to {
        stroke-dasharray: 0;
        stroke-dashoffset: 0;
        opacity: 0.5;
      }
    }
  </style>
  <g id="svg-loading-fast">
    <text x="50" y="55" style="text-anchor: middle; fill: #5CC9F5; font-size: 14px;">加载中</text>
    <circle cx="50" cy="50" r="40" style="fill: none; stroke: #5CC9F5; stroke-width: 5px; stroke-dasharray: null; stroke-dashoffset: null" />
  </g>
</svg>
复制代码

基础版比较单调,这个加速版就先不设置默认的stroke-dasharray和stroke-dashoffset,直接在animation动画设置后逐渐减少

3、滚动版

代码:

<svg width="100" height="100">
  <style>
    #svg-loading-rolling circle {
      stroke-dasharray: 300;
      stroke-dashoffset: 300;
      animation: svg-loading-rolling 1.5s linear infinite;
    }
    @keyframes svg-loading-rolling {
      from {
        stroke-dasharray: 600;
        stroke-dashoffset: 600;
        transform: rotate(0);
        transform-origin: 50px 50px;
      }
      to {
        stroke-dashoffset: 0;
        transform: rotate(720deg);
        transform-origin: 50px 50px;
        opacity: 0.1;
      }
    }
  </style>
  <g id="svg-loading-rolling">
    <text x="50" y="55" style="text-anchor: middle; fill: #5CC9F5; font-size: 14px;">加载中</text>
    <circle cx="50" cy="50" r="40" style="fill: none; stroke: #5CC9F5; stroke-width: 5px; stroke-dasharray: null; stroke-dashoffset: null" />
  </g>
</svg>
复制代码

除了加速版,还可以弄一个滚动版,利用css3里的rotate为circle添加一个旋转动画之后,整体的加载效果比基础版流畅多了

4、心电图版

代码:

<svg width="100" height="100">
  <style>
    #svg-loading-polyline {
      stroke-dasharray: 400;
      stroke-dashoffset: 400;
      animation: svg-loading-polyline 1s linear infinite;
    }
    @keyframes svg-loading-polyline {
      from {
        stroke-dasharray: 600;
        stroke-dashoffset: 600;
      }
      to {
        stroke-dashoffset: 0;
        opacity: 0.1;
      }
    }
  </style>
  <polyline id="svg-loading-polyline" points="10 90, 30 10, 40 70, 45 60, 55 80, 65 30, 80 80, 90 90" style="fill: none; stroke: #5CC9F5; stroke-width: 3px; stroke-dasharray: null; stroke-dashoffset: null" />
</svg>
复制代码

当然也可以用折线做一个心电图版,效果也不错

总结

利用stroke-dasharray和stroke-dashoffset这两个属性制作动效相当实用,结合css3相关动画,可以快速制作简单美观的动画效果

转载于:https://juejin.im/post/5c94e1e66fb9a070ef60a666

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值