【发布时间】:2011-03-30 17:18:59
【问题描述】:
我需要为 SVG 元素的属性设置动画,该元素没有 CSS 对应项 -
<circle cx="..." />我怎样才能做到这一点?
或者,我可以使用替代方案,例如如果我可以使用 CSS 的 top 或类似方法为 <g/> 制作动画。
有什么经验吗?
谢谢,昂德拉
请注意,这不是 How can I animate an attribute value (not style property) with jQuery? 的重复,因为那是关于 HTML。
更新
最后,我做了一个手动动画,如SVG draggable using JQuery and Jquery-svg 。
jQuery 解决方案仍然受欢迎。
无论如何,这给我带来了其他问题 - 单位不匹配。 evt.clientX 以像素为单位,但circle.cx.baseVal.value 以某些相对单位为单位。所以当我这样做时
onmousedown="
this.moving=true;
this.pt0 = {x: evt.clientX, y: evt.clientY};
this.origPos = {x: this.cx.baseVal.value, y: this.cy.baseVal.value};
"
onmouseup="this.moving=false;"
onmouseout="this.moving=false;"
onmouseover="this.moving=false;"
onmousemove="if( this.moving ){
this.cx.baseVal.value = this.origPos.x + (evt.clientX - this.pt0.x);
}
如果<embed> 比 SVG 的“自己”大小大 3 倍,那么圆的移动速度比指针快 3 倍。
更新
我什至尝试过
this.origPos = {
x: this.cx.baseVal.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX),
y: this.cy.baseVal.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX)
};
...
this.cx.baseVal.newValueSpecifiedUnits(
SVGLength.SVG_LENGTHTYPE_PX, this.origPos.x + (evt.clientX - this.pt0.x) );
但是convertToSpecifiedUnits()返回undefined,这似乎是http://www.w3.org/TR/SVG/types.html#InterfaceSVGLength的实现不足。
【问题讨论】:
标签: jquery animation svg attributes jquery-animate