菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

SVG 动画


SVG 动画

SVG 元素可以动画化。

在 SVG 中,我们有四个动画元素来设置或动画化 SVG 图形

  • <set>
  • <animate>
  • <animateTransform>
  • <animateMotion>

SVG <set>

<set> 元素用于在指定持续时间内设置属性的值。

在此示例中,我们创建一个红色圆圈,其半径初始为 25,3 秒后半径将设置为 50

Sorry, your browser does not support inline SVG.

这是 SVG 代码

示例

<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="25" style="fill:red;">
  <set attributeName="r" to="50" begin="3s" />
</svg>
自己动手试一试 »

代码解释

  • <set> 元素中的 attributeName 属性定义要更改的属性
  • <set> 元素中的 to 属性定义属性的新值
  • <set> 元素中的 begin 属性定义动画何时开始

SVG <animate>

<animate> 元素用于动画化元素的属性。

<animate> 元素应嵌套在目标元素内。

在此示例中,我们创建一个红色圆圈。我们动画化 cx 属性从 50 到 90%。这意味着圆圈将从左向右移动

Sorry, your browser does not support inline SVG.

这是 SVG 代码

示例

<svg width="100%" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="50" style="fill:red;">
    <animate
      attributeName="cx"
      begin="0s"
      dur="8s"
      from="50"
      to="90%"
      repeatCount="indefinite" />
  </circle>
</svg>
自己动手试一试 »

代码解释

  • attributeName 属性定义要动画化的属性
  • begin 属性定义动画何时开始
  • dur 属性定义动画的持续时间
  • from 属性定义起始值
  • to 属性定义结束值
  • repeatCount 属性定义动画播放的次数

带冻结的 SVG <animate>

在此示例中,我们希望红色圆圈在到达最终位置时冻结(停止)(而不是弹回起始位置)

Sorry, your browser does not support inline SVG.

这是 SVG 代码

示例

<svg width="100%" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="50" style="fill:red;">
    <animate
      attributeName="cx"
      begin="0s"
      dur="8s"
      from="50"
      to="90%"
      fill="freeze" />
  </circle>
</svg>
自己动手试一试 »

代码解释

  • fill="freeze" 属性定义动画在到达最终位置时应冻结


SVG <animateTransform>

<animateTransform> 元素用于动画化目标元素的 transform 属性。

<animateTransform> 元素应嵌套在目标元素内。

在此示例中,我们创建一个将旋转的红色矩形

Sorry, your browser does not support inline SVG.

这是 SVG 代码

示例

<svg width="200" height="180" xmlns="http://www.w3.org/2000/svg">
  <rect x="30" y="30" height="110" width="110" style="stroke:green;fill:red">
    <animateTransform
      attributeName="transform"
      begin="0s"
      dur="10s"
      type="rotate"
      from="0 85 85"
      to="360 85 85"
      repeatCount="indefinite" />
  </rect>
</svg>
自己动手试一试 »

代码解释

  • attributeName 属性动画化 <rect> 元素的 transform 属性
  • begin 属性定义动画何时开始
  • dur 属性定义动画的持续时间
  • type 属性定义变换的类型
  • from 属性定义起始值
  • to 属性定义结束值
  • repeatCount 属性定义动画播放的次数

SVG <animateMotion>

<animateMotion> 元素设置元素沿运动路径的移动方式。

<animateMotion> 元素应嵌套在目标元素内。

在此示例中,我们创建一个矩形和一段文本。这两个元素都具有相同的路径 <animateMotion> 元素

It's SVG! Sorry, your browser does not support inline SVG.

这是 SVG 代码

示例

<svg width="100%" height="150" xmlns="http://www.w3.org/2000/svg">
  <rect x="45" y="18" width="155" height="45" style="stroke:green;fill:none;">
    <animateMotion
      path="M0,0 q60,100 100,0 q60,-20 100,0"
      begin="0s"
      dur="10s"
      repeatCount="indefinite" />
  </rect>
  <text x="50" y="50" style="font-family:Verdana;font-size:32">它是 SVG!
    <animateMotion
      path="M0,0 q60,100 100,0 q60,-20 100,0"
      begin="0s"
      dur="10s"
      repeatCount="indefinite" />
</text>
</svg>
自己动手试一试 »

代码解释

  • path 属性定义动画的路径
  • begin 属性定义动画何时开始
  • dur 属性定义动画的持续时间
  • repeatCount 属性定义动画播放的次数

×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持