在悬停时修改SVG路径

时间:2014-07-11 07:28:59

标签: javascript jquery css svg

我想在悬停时更改svg路径坐标,我可以不使用D3js吗?

SVG是我在sketchapp上制作的一种图表,这里是代码:

    <svg class="light-graph" viewBox="0 0 1440 269">
        <g id="ANALYTICS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
            <g sketch:type="MSArtboardGroup" transform="translate(0.000000, -331.000000)" id="header" fill="#3797D1" opacity="0.323868937">
                <g sketch:type="MSLayerGroup">
                    <g id="bg-graph" sketch:type="MSShapeGroup">
                        <path d="M1123.39844,467.39269 L869.398438,569.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z"></path>
                    </g>
                </g>
            </g>
        </g>
    </svg>

我想要做的是图表在悬停时平滑过渡。该图表将作为封面放在标题上。

谢谢!

1 个答案:

答案 0 :(得分:2)

你可以用SMIL做到这一点。

<path d="M1123.39844,467.39269 L869.398438,569.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z">
    <animate begin="mouseover" attributeName="d" to="M1123.39844,467.39269 L869.398438,69.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z" dur="0.3s" fill="freeze" />
    <animate begin="mouseout" attributeName="d" to="M1123.39844,467.39269 L869.398438,569.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z" dur="0.3s" fill="freeze" />
</path>

begin属性是您要激活动画的事件,而to是路径最终的结果。

填充=&#34;冻结&#34;确保如果将鼠标放在形状上,它将保持在动画位置。

这是我在Firefox中测试的完整独立示例。

<svg xmlns="http://www.w3.org/2000/svg" class="light-graph" viewBox="0 0 1440 269">
    <g id="ANALYTICS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <g transform="translate(0.000000, -331.000000)" id="header" fill="#3797D1" opacity="0.323868937">
            <g>
                <g id="bg-graph">
                    <path d="M1123.39844,467.39269 L869.398438,569.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z">
                        <animate begin="mouseover" attributeName="d" to="M1123.39844,467.39269 L869.398438,69.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z" dur="0.3s" fill="freeze" />
                        <animate begin="mouseout" attributeName="d" to="M1123.39844,467.39269 L869.398438,569.515104 L720,331.388951 L559.398438,369.409092 L379.398437,587.524641 L123.398438,427.439835 L0,600 L1440,600 L1123.39844,467.39269 Z" dur="0.3s" fill="freeze" />
                    </path>
                </g>
            </g>
        </g>
    </g>
</svg>