简体   繁体   English

动画a <g> SVG中的CSS元素

[英]Animate a <g> element in SVG with CSS

In an SVG I have a <g class="building"> that I'd like to move upwards on hover. 在SVG中,我有一个<g class="building"> ,我想在悬停时向上移动。

    <svg x="0px" y="0px"
         viewBox="0 0 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve">
   <g class="building">
    <rect id="XMLID_1_" x="128.6" y="296.2"/>
 </g>
    </svg>

If the was a <div class="building"> , the CSS that I'd use would be: 如果是<div class="building"> ,我使用的CSS将是:

.building {
position: relative;
top: -20px;
}

As position and top does not work in SVG, I'll obviously have to do this another way 由于positiontop在SVG中不起作用,我显然必须以另一种方式做到这一点

How do I move up <g class="building"> 20px? 我如何向上移动<g class="building"> 20px?

Here's a fiddle 这是一个小提琴

You can use a CSS transform to move it. 您可以使用CSS转换来移动它。 Be careful as you've the same class defined for the g and the rect, you might want to reconsider that. 要小心,因为你为g和rect定义了相同的类,你可能想重新考虑一下。

  .building{fill:#72B62B;stroke:#000000;stroke-miterlimit:10;} .ground{fill:#030203;stroke:#000000;stroke-miterlimit:10;} .building:hover { transform: translateY(-30px); } 
 <svg 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 595.3 841.9" style="enable-background:new 0 0 595.3 841.9;" xml:space="preserve"> <g class="building"> <rect id="XMLID_2_" x="21.6" y="645" class="building" width="550" height="74.3"/> </g> 

You dont need css to animate svg example heer:
<circle id="orange-circle" r="30" cx="50" cy="50" fill="orange" />
<animate 
    xlink:href="#orange-circle"
    attributeName="cx"
    from="50"
    to="450" 
    dur="5s"
    begin="click"
    fill="freeze" 
    d="circ-anim" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM