如何使用CSS或JavaScript动画或转换SVG路径?

时间:2016-06-15 13:02:27

标签: javascript html css svg svg-path

我在Adobe Illustrator中创建了一个SVG图像,其中包含以下代码:

<svg id="logo_top" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1440 1440" enable-background="new 0 0 1440 1440"
     xml:space="preserve" height="512px" width="512px">
  <g>
    <path id="path1" d="M1140.475,774.096c4.692,3.807,12.367,5.709,23.023,5.709c10.655,0,18.326-1.902,23.023-5.709
                                     c4.691-3.805,7.039-9.764,7.039-17.885c0-8.117-2.348-14.08-7.039-17.887c-4.697-3.805-12.368-5.707-23.023-5.707
                                     c-10.656,0-18.331,1.902-23.023,5.707c-4.697,3.807-7.04,9.77-7.04,17.887C1133.435,764.332,1135.777,770.291,1140.475,774.096z" />
    <path id="path2" d="M1140.475,716.163c4.693,3.806,12.367,5.708,23.023,5.708s18.326-1.902,23.023-5.708c4.691-3.806,7.039-9.764,7.039-17.886
                                     c0-8.117-2.348-14.08-7.039-17.886c-4.697-3.806-12.367-5.708-23.023-5.708s-18.33,1.902-23.023,5.708
                                     c-4.697,3.806-7.039,9.769-7.039,17.886C1133.436,706.399,1135.777,712.357,1140.475,716.163z" />
    <path id="path3" d="M1140.475,658.23c4.693,3.805,12.367,5.707,23.023,5.707s18.326-1.902,23.023-5.707c4.691-3.807,7.039-9.764,7.039-17.887
                                     c0-8.116-2.348-14.08-7.039-17.885c-4.697-3.807-12.367-5.709-23.023-5.709s-18.33,1.902-23.023,5.709
                                     c-4.697,3.805-7.039,9.769-7.039,17.885C1133.436,648.467,1135.777,654.424,1140.475,658.23z" />
  </g>
</svg>

我想要实现的是当我将鼠标悬停在SVG上时,让最顶部的点向上移动。到目前为止,我尝试了以下内容:

  1. transform: translate(0, 10px);

  2. position: relative;bottom: 10px;

  3. margin-bottom: 10px;

  4. padding-bottom: 10px;

  5. 不幸的是,以上都没有奏效,我现在对于为什么svg元素需要这种特殊处理并且不像其他所有“普通”HTML元素那样有用而感到好奇。

    我知道可以使用animatetransform制作多边形动画。但它在路径上似乎也不适合我。

    <animateTransform attributeName="transform"
                          attributeType="XML"
                          type="rotate"
                          from="0 60 70"
                          to="360 60 70"
                          dur="10s"
                          repeatCount="indefinite"/>
    

    为了向上移动最上面的点,我必须改变什么属性?

1 个答案:

答案 0 :(得分:0)

为了操纵路径点,需要使用JavaScript。以下代码实现了所需的结果:

$("#path1").css("transform", "translate(0, -100px)");