无css,用animate实现svg动画

本文使用 svganimate 实现2个简单常用的动画,svg原理、怎么画图什么的,切图仔讲不明白啦。

if (!you['svg']) return🤣

animate 基础使用

html 复制代码
<svg width="200" height="200">
  <circle cx="50" cy="50" r="20" fill="blue">
    <animate attributeName="cx"
       from="50"
       to="150"
       dur="1s"
       repeatCount="indefinite"
    />
  </circle>
</svg>

简单做了一个小球移动,其中:

  • attributeName属性指定了要做动画的属性,这里是圆形的cx属性,即圆心的横坐标。
  • from属性指定了属性动画的起始值,这里是50
  • to属性指定了属性动画的结束值,这里是150
  • dur属性指定了动画的持续时间,这里是1s,即1秒。
  • repeatCount属性指定了动画的重复次数,这里是indefinite,表示无限重复。

上面的 fromto 还可以改为 values

html 复制代码
<svg width="200" height="200">
  <circle cx="50" cy="50" r="20" fill="blue">
    <animate 
        attributeName="cx"
        values="50;150;50"
        dur="1s"
        repeatCount="indefinite"
    />
  </circle>
</svg>

values 的值为 50;150;50,这样小球的 cx 会在1秒内从 50150,然后回到 50,就有了一个往返效果

loading动画

创建一个 svg,然后画3个柱子

html 复制代码
<svg width="200" height="200">
  <rect x="0" y="0" width="8" height="20" fill="#888"></rect>
  <rect x="16" y="0" width="8" height="20" fill="#888"></rect>
  <rect x="32" y="0" width="8" height="20" fill="#888"></rect>
</svg>

接下来给这些柱子添加修改高度的动画,通过 begin 设置触发的时间点

html 复制代码
<svg width="200" height="200">
  <rect x="0" y="0" width="8" height="20" fill="#888">
      <animate
          attributeName="height"
          values="20; 40; 20"
          begin="0s"
          dur="0.6s"
          repeatCount="indefinite"
      />
  </rect>
  <rect x="16" y="0" width="8" height="20" fill="#888">
      <animate
          attributeName="height"
          values="20; 40; 20"
          begin="0.15s"
          dur="0.6s"
          repeatCount="indefinite"
      />
  </rect>
  <rect x="32" y="0" width="8" height="20" fill="#888">
      <animate
          attributeName="height"
          values="20; 40; 20"
          begin="0.3s"
          dur="0.6s"
          repeatCount="indefinite"
      />
  </rect>
</svg>

改了这些属性后能动,但挺丑,修改下 yopacity 让它看起来更有动感

html 复制代码
<svg width="200" height="200">
    <rect x="0" y="20" width="8" height="20" fill="#888" opacity="0.2">
        <animate
            attributeName="opacity"
            values="0.2; 1; 0.2"
            begin="0s"
            dur="0.6s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="height"
            values="20; 40; 20"
            begin="0s"
            dur="0.6s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="y"
            values="20; 10; 20"
            begin="0s"
            dur="0.6s"
            repeatCount="indefinite"
        />
    </rect>
    <rect x="16" y="20" width="8" height="20" fill="#888" opacity="0.2">
        <animate
            attributeName="opacity"
            values="0.2; 1; 0.2"
            begin="0.15s"
            dur="0.6s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="height"
            values="20; 40; 20"
            begin="0.15s"
            dur="0.6s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="y"
            values="20; 10; 20"
            begin="0.15s"
            dur="0.6s"
            repeatCount="indefinite"
        />
    </rect>
    <rect x="32" y="20" width="8" height="20" fill="#888" opacity="0.2">
        <animate
            attributeName="opacity"
            values="0.2; 1; 0.2"
            begin="0.3s"
            dur="0.6s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="height"
            values="20; 40; 20"
            begin="0.3s"
            dur="0.6s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="y"
            values="20; 10; 20"
            begin="0.3s"
            dur="0.6s"
            repeatCount="indefinite"
        />
    </rect>
</svg>

总体看上去还行,过得去~

波纹动画

就直接给代码吧

html 复制代码
<svg width="200" height="200">
    <circle cx="100" cy="100" r="10" fill="blue" opacity="0">
        <animate
            attributeName="opacity"
            values="1;0"
            dur="1s"
            repeatCount="indefinite"
        />
        <animate
            attributeName="r"
            values="10;30"
            dur="1s"
            repeatCount="indefinite"
        />
    </circle>
    <circle cx="100" cy="100" r="10" fill="blue" opacity="0">
        <animate
            attributeName="opacity"
            values="1;0"
            dur="1s"
            repeatCount="indefinite"
            begin="0.2s"
        />
        <animate
            attributeName="r"
            values="10;30"
            dur="1s"
            repeatCount="indefinite"
            begin="0.2s"
        />
    </circle>
    <circle cx="100" cy="100" r="10" fill="blue" opacity="0">
        <animate
            attributeName="opacity"
            values="1;0"
            dur="1s"
            repeatCount="indefinite"
            begin="0.4s"
        />
        <animate
            attributeName="r"
            values="10;30"
            dur="1s"
            repeatCount="indefinite"
            begin="0.4s"
        />
    </circle>
</svg>

总结

这些都可以通过 css 实现,只是个人感觉直接 svg 一把梭简单一些😀。除了上述的 animate,还可以通过 dasharraydashoffset 进行动态描边。总的来说 svg 还是有很多需要学习的地方。

更多属性见:- SVG:可缩放矢量图形 | MDN (mozilla.org)

相关推荐
E_ICEBLUE1 天前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
Highcharts.js1 天前
如何使用Highcharts SVG渲染器?
开发语言·javascript·python·svg·highcharts·渲染器
谜亚星1 个月前
SVG学习(五)
前端·svg
harrain1 个月前
前端svg精微操作局部动态改变呈现工程网架状态程度可视播放效果
前端·svg·工程网架图
我真的叫奥运1 个月前
scss mixin svg 颜色控制 以及与 png 方案对比讨论
前端·svg
harrain1 个月前
html里引入使用svg的方法
前端·svg
咬人喵喵1 个月前
SVG 答题类互动模板汇总(共 16 种/来自 E2 编辑器)
编辑器·svg·e2 编辑器
咬人喵喵1 个月前
16 类春节核心 SVG 交互方案拆解(E2 编辑器实战)
前端·css·编辑器·交互·svg
李少兄1 个月前
简单讲讲 SVG:前端开发中的矢量图形
前端·svg
咬人喵喵1 个月前
文生图:AI 是怎么把文字变成画的?
人工智能·编辑器·svg