javascript - 使用CSS(或JavaScript)代替SMIL

标签 javascript html css svg smil

我在 Chrome 控制台中看到以下警报:

SVG's SMIL animations (<animate>, <set>, etc.) are deprecated and will be removed. Please use CSS animations or Web animations instead.

所以我想用CSS替换SMIL。
我可以在 CSS 中做一些事情,但我不知道如何做这一件事。 我的问题是我在 SMIL 中对 d 属性进行了动画处理,但我不知道我可以/如何在 CSS 中做到这一点。 这是我的代码:

svg{
	position: fixed;
}
<svg width="500" height="500" viewBox="0 0 500 500">
	<path id="rect" fill="lightblue" d="M10 10 L30 10 L30 30 L10 30 Z"/>
	<animate xlink:href="#rect"
		attributeName="d"
		attributeType="XML"
		values="M20 20 L30 20 L40 40 L20 40 Z;
		M10 10 L30 10 L40 30 L10 40 Z;
		M10 10 L30 10 L30 30 L10 30 Z;
		M20 20 L30 20 L40 40 L20 40 Z"
		begin="0s"
		dur="2s" 
		repeatCount="indefinite"
	/>
</svg>

谢谢!

P.S.:如果 CSS 无法做到这一点,或者 JavaScript 无法做到更快,那么 JavaScript 也可以。

最佳答案

我编写了一些 JavaScript 来在路径之间进行插值。这是非常容易使用。您只需将脚本添加到页面并编写以下行:

var rect = new bigT('#rect');
rect.indefinite(true)
        .easing("linear")
        .duration(2000)
        .setPathKeyframes(
        "M20 20 L30 20 L40 40 L20 40 Z",
        "M10 10 L30 10 L40 30 L10 40 Z",
        "M10 10 L30 10 L30 30 L10 30 Z",
        "M20 20 L30 20 L40 40 L20 40 Z");
rect.startAnimation();

您可以在这里下载bigT.js

此外,这是您的 svg 的预览:http://jsfiddle.net/r99fkndq/3/

或者,您可以使用更广泛的库,例如 D3.js。

关于javascript - 使用CSS(或JavaScript)代替SMIL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32410474/

相关文章:

PHP 登录系统在我点击登录后不显示任何内容

css - 在开发自己的模板时注释掉未使用的 CSS?

javascript - 印地语在 Chrome 浏览器中呈现不正确

javascript - 使用 debug 在 Node.js 中打印函数名称和参数

javascript - MapBox GL CSP 版本不渲染图 block

javascript - 基于多选的 jQuery 条件

javascript - 如何仅在右侧应用阴影并隐藏底部边框?

javascript - 如果 Servlet 隧道与 html 文件不在同一主机/服务器上,我可以调用 Guacamole.HTTPTunnel ("tunnel"吗?

javascript - 如何更新在我的 javascript 代码中单击的 div

javascript - 将 jquery 值绑定(bind)到特定的 DIV 高度