旋转动画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文档

<think>好的,我需要帮助用户实现SVG旋转动画效果,使用CSS或JavaScript。首先,我需要回忆一下相关的知识,确保回答正确。 用户提到了引用[1]和[2],其中提到CSS3和SVG结合使用,可以创建动画,而且不需要JavaScript库。所以应该优先考虑CSS方法,因为它更简单,加载更快。不过用户也问到了JavaScript,所以可能需要两种方法。 首先,SVG旋转动画可以通过CSS的transform属性实现。但需要注意,SVG元素的旋转中心可能和普通HTML元素不同,需要使用transform-origin或者直接在transform中指定。另外,SVG元素本身可以使用<animateTransform>标签来实现动画,这是纯SVG的方式,但用户可能更倾向于CSS或JS。 对于CSS方法,关键点在于使用@keyframes定义旋转动画,然后应用到SVG元素上。例如,创建一个旋转的keyframe,设置from和to或者百分比的变化。需要确保旋转的中心点是SVG元素的中心,否则会绕默认点(可能是左上角)旋转。这时候可能需要设置transform-origin为50% 50%,或者在transform中使用rotate(角度, 中心x, 中心y)的语法,但CSS的rotate函数是否支持指定中心点呢?可能需要查证。不过通常使用transform-origin来调整中心点。 另外,如果用户使用的是<circle>或其他形状,包裹在<svg>标签内,直接应用CSS动画应该可行。例如: .svg-element { animation: rotate 2s linear infinite; } @keyframes rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } 但是transform-origin可能需要设置为center,或者具体的坐标,特别是当SVG元素的位置不是居中时。 另一个方法是使用SVG的<animateTransform>元素,作为子元素添加到需要动画SVG元素里。例如: <circle cx="50" cy="50" r="20"> <animateTransform attributeName="transform" type="rotate" from="0 50 50" to="360 50 50" dur="2s" repeatCount="indefinite"/> </circle> 这里,rotate的参数是角度,后面跟着旋转中心的x和y坐标。这种方法不依赖CSS,直接在SVG中定义动画,但可能更复杂一些。不过用户可能想用CSS或JS,所以需要比较这两种方法。 对于JavaScript方法,可以通过定时器(如requestAnimationFrame)动态修改元素的transform属性,或者使用Web Animations API。例如,用JS控制旋转角度随时间变化,更新元素的transform属性。这种方法更灵活,但需要编写更多代码,可能增加复杂性。 现在需要将以上思路整理成步骤,给出示例代码,并确保引用正确。用户的问题是关于如何创建旋转动画,所以需要分CSS和JS两种方式说明。 引用[1]提到CSS3和SVG结合的技术要点,所以CSS方法应优先,并且引用[1]作为参考。引用[2]提到使用CSS可以减少加载时间,避免JS库,所以这也是支持使用CSS的理由。 可能的步骤: 1. 使用CSS动画: - 定义@keyframes旋转动画。 - 应用动画SVG元素,设置transform-origin为正确中心点。 - 示例代码展示一个旋转SVG圆圈。 2. 使用SVG的<animateTransform>: - 内嵌在SVG元素中的动画标签。 - 指定旋转参数和持续时间。 3. 使用JavaScript: - 通过动态改变transform属性实现动画。 - 使用requestAnimationFrame更新角度。 需要注意,当使用CSS的transform时,SVG元素的transform可能与HTML元素的transform不同,可能需要使用CSS的transform属性,或者SVG的transform属性。例如,在SVG中,元素可能原本就有transform属性,但应用CSS的transform可能会覆盖或者需要正确拼接。 另外,旋转中心的确定很重要。例如,如果SVG元素的中心在(50,50),则transform-origin应该设置为50px 50px,或者使用百分比,但可能需要根据具体情况调整。 现在,编写具体的示例代码: 对于CSS方法: 假设SVG元素是一个圆形,包裹在svg容器中: <svg width="100" height="100"> <circle id="circle" cx="50" cy="50" r="40" fill="none" stroke="#000" stroke-width="4"/> </svg> 应用CSS: #circle { transform-origin: 50px 50px; /* 或者使用 center */ animation: rotate 2s linear infinite; } @keyframes rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } 或者,如果transform-origin设置为50% 50%,可能需要测试,因为对于SVG元素,百分比是基于元素自身的尺寸还是视口?可能更可靠的是使用具体像素值,或者将元素放在视口中心。 另一种情况,如果SVG视口是100x100,圆心在(50,50),则transform-origin: center可能有效,即: #circle { transform-origin: center; animation: rotate 2s linear infinite; } 这样可能更简洁。 对于SVG animateTransform方法: <svg width="100" height="100"> <circle cx="50" cy="50" r="40" fill="none" stroke="#000" stroke-width="4"> <animateTransform attributeName="transform" type="rotate" from="0 50 50" to="360 50 50" dur="2s" repeatCount="indefinite"/> </circle> </svg> 这种方法不需要CSS,直接内嵌动画。 对于JavaScript方法: 可以使用requestAnimationFrame来动态更新旋转角度: const circle = document.getElementById('circle'); let angle = 0; function animate() { angle += 2; circle.style.transform = `rotate(${angle}deg)`; requestAnimationFrame(animate); } animate(); 但同样需要注意transform-origin的设置,否则旋转中心可能不正确。所以可能需要先设置transform-origin到中心点: circle.style.transformOrigin = '50px 50px'; 或者使用JS设置: circle.style.transformOrigin = 'center'; 这样结合起来,才能正确绕中心旋转。 总结以上,用户的问题需要分步骤说明,提供不同方法,并附上代码示例。同时引用提到的参考资料,尤其是引用[1]中的CSS3和SVG结合的技术要点,以及引用[2]中关于加载时间的优势。 最后,生成相关问题,如不同方法的优缺点、如何控制动画速度、如何暂停动画等。</think>### 使用CSS或JavaScript创建SVG旋转动画的方法 #### 方法一:CSS动画(推荐) 通过CSS的`@keyframes`定义旋转动画,结合`transform-origin`确保旋转中心与SVG元素中心对齐[^1]。 **步骤:** 1. **定义SVG结构**: ```html <svg width="100" height="100"> <circle id="circle" cx="50" cy="50" r="40" fill="none" stroke="#000" stroke-width="4"/> </svg> ``` 2. **添加CSS动画**: ```css #circle { transform-origin: center; /* 或使用像素值:transform-origin: 50px 50px; */ animation: rotate 2s linear infinite; } @keyframes rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } ``` #### 方法二:SVG原生动画(无需代码) 使用SVG的`<animateTransform>`标签直接嵌入动画: ```html <svg width="100" height="100"> <circle cx="50" cy="50" r="40" fill="none" stroke="#000" stroke-width="4"> <animateTransform attributeName="transform" type="rotate" from="0 50 50" to="360 50 50" dur="2s" repeatCount="indefinite" /> </circle> </svg> ``` - `from="0 50 50"`表示初始角度为0度,旋转中心为`(50,50)`。 - `dur="2s"`设定动画周期为2秒。 #### 方法三:JavaScript动态控制 通过`requestAnimationFrame`或`Web Animations API`实现更灵活的交互: ```html <svg width="100" height="100"> <circle id="circle" cx="50" cy="50" r="40" fill="none" stroke="#000" stroke-width="4"/> </svg> <script> const circle = document.getElementById('circle'); let angle = 0; // 设置旋转中心 circle.style.transformOrigin = 'center'; function animate() { angle += 2; circle.style.transform = `rotate(${angle}deg)`; requestAnimationFrame(animate); } animate(); </script> ``` #### 对比与注意事项: - **CSS动画**:代码简洁,性能优化更好,适合简单动画[^2]。 - **SVG原生动画**:无需外部代码,但灵活性较低。 - **JavaScript**:适合需要动态控制的场景,如暂停、调速等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值