旋转动画svg

需求
1.实现元素沿椭圆轨迹均匀旋转
2.鼠标点击事件、移入暂停运动
3.元素由远到近逐渐增大
在这里插入图片描述

旋转动画的实现思路:
1.path指定椭圆轨迹路径,stroke="lightgrey"可以设置轨迹颜色,在测试时添加颜色方便修改路径。

<path d="M10 130 
        A 120 80, 0, 0, 1, 360 130 
        A 120 60, 0, 0, 1, 10 130" stroke-width="2" fill="none" id="theMotionPath" />

2.animateMotion元素可以让SVG图形沿着指定的path路径运动,实现多个元素均匀旋转,控制好dur和begin时间。

<animateMotion dur="16s" begin="0s" fill="freeze" repeatCount="indefinite">
     <mpath xlink:href="#theMotionPath" />
</animateMotion>

3.实现由远到近元素的大小缩放,values值表示缩放的比例。

<animateTransform attributeName="transform" begin="0s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9" repeatCount="indefinite" />
完整代码如下:

<body>
  <div class="container">
    <svg width="100%" height="100%" id="svg_an">
      <g id="layer0">
        <path d="M10 130 
        A 120 80, 0, 0, 1, 360 130 
        A 120 60, 0, 0, 1, 10 130" stroke-width="2" fill="none" id="theMotionPath" />
      </g>
      <g id="layer1" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item1" width="70" height="70">
        </image>
        <animateMotion dur="16s" begin="0s" fill="freeze" repeatCount="indefinite">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="0s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>1</text>
      </g>
      <g id="layer2" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item2" width="70" height="70">
        </image>
        <animateMotion repeatCount="indefinite" dur="16s" begin="-2s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-2s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>2</text>
      </g>
      <g id="layer3" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item3" width="70" height="70">
        </image>
        <animateMotion dur="16s" repeatCount="indefinite" begin="-4s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-4s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>3</text>
      </g>
      <g id="layer4" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item4" width="70" height="70">
        </image>
        <animateMotion dur="16s" repeatCount="indefinite" begin="-6s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-6s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>4</text>
      </g>
      <g id="layer5" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item5" width="70" height="70">
        </image>
        <animateMotion dur="16s" repeatCount="indefinite" begin="-8s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-8s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>5</text>
      </g>
      <g id="layer6" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item6" width="70" height="70">
        </image>
        <animateMotion dur="16s" repeatCount="indefinite" begin="-10s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-10s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>6</text>
      </g>
      <g id="layer7" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item7" width="70" height="70">
        </image>
        <animateMotion dur="16s" repeatCount="indefinite" begin="-12s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-12s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>7</text>
      </g>
      <g id="layer8" cursor="pointer" onmouseover="pauseAn()" onmouseout="unpauseAn()">
        <image xlink:href="img/ball.jpg" id="item8" width="70" height="70">
        </image>
        <animateMotion dur="16s" repeatCount="indefinite" begin="-14s">
          <mpath xlink:href="#theMotionPath" />
        </animateMotion>
        <animateTransform attributeName="transform" begin="-14s" dur="16s" type="scale" values="0.9;0.5;0.7;0.9;1;0.9"
          repeatCount="indefinite" />
        <text>8</text>
      </g>
    </svg>
  </div>
  <script src="js/snap.svg.js" type="text/javascript"></script>
  <script src="js/index.js" type="text/javascript"></script>
</body>
//动画的暂停与开始
var svg1 = document.getElementById("svg_an");
function pauseAn() {
  svg1.pauseAnimations();
}
function unpauseAn() {
  svg1.unpauseAnimations();
}

//鼠标滑入元素缩放及点击事件
var svg = Snap("#svg_an")
for (var i = 1; i <= 8; i++) {
  svg.select("#item" + i).mouseover(function () {
    this.transform("s" + [1.2, 1.2])
  })
  svg.select("#item" + i).mouseout(function () {
    this.transform("s" + [1, 1])
  })
  svg.select("#item" + i).click(function () {
    console.log(this)
    //console.log(this.node.href.baseVal)
  })
}

参考
SVG 动画精髓
超级强大的SVG SMIL animation动画详解
SVG动画
Snap.svg中文API文档

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SVG动画是一种使用Scalable Vector Graphics (可缩放矢量图形)格式来创建动画效果的技术。与其他常见的图像格式(如JPEG或PNG)不同,SVG具有矢量特性,可以缩放并保持清晰度。 要加载SVG动画,首先需要将SVG文件嵌入到HTML文档中。可以通过`<object>`或`<embed>`标签来实现。例如: ```html <object type="image/svg+xml" data="animation.svg"> Your browser does not support SVG </object> ``` 在加载完成后,可以使用CSS或JavaScript来控制SVG动画的行为。可以添加CSS样式来改变动画的外观,或者使用JavaScript来控制动画的播放、暂停和重放。 要实现加载动画,可以在SVG文件中添加动画元素和属性。例如,可以使用`<animate>`元素来定义属性的动画效果。例如,要创建一个从左到右移动的动画效果,可以使用以下代码: ```html <rect width="100" height="50"> <animate attributeName="x" from="0" to="200" dur="1s" fill="freeze" repeatCount="indefinite" /> </rect> ``` 在上面的示例中,`<rect>`元素定义了一个矩形,`<animate>`元素定义了矩形的x属性动画。`attributeName`属性指定要动画化的属性,`from`和`to`属性指定属性的起始值和终止值,`dur`属性指定动画的持续时间,`fill`属性指定动画结束后属性的保持方式,`repeatCount`属性指定动画的重复次数。 通过调整属性和添加其他SVG元素和动画,可以创造出各种各样的加载动画效果。无论是旋转、缩放、渐变还是路径动画,都可以通过SVG轻松实现。 总之,加载SVG动画涉及将SVG文件嵌入到HTML文档中,并使用CSS或JavaScript来控制动画的行为。通过使用SVG的矢量特性和丰富的动画元素和属性,可以创建各种各样的引人注目的加载动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值