javascript - SVG 动画在以编程方式设置样式时有效,但在仅使用样式表时无效

标签 javascript css svg

我有一个 svg 文档,其中包含样式表。使用样式表我可以设置矩形的填充(样式表正在工作)。我想做的是设置一个变色动画。我有以下样式表:

@keyframes blinking-effect {
    0% {
        stroke: rgba(255, 255, 0, 1);
        fill: rgba(0, 220, 0, 1);
    }
    100% {
        stroke: rgba(255, 255, 255, 0);
        fill: rgba(0, 0, 220, 1);
    }
}
.animation {
    animation-name: "blinking-effect";
    animation-duration: "0.5s";
    animation-iteration-count: "infinite";
    animation-timing-function: "ease-in-out";
    animation-direction: "alternate";
}


<rect id="rect1" x="0" y="0" width="50%" y="50%" class="animation " />

我不知道为什么这不起作用。但是,当我执行以下代码时,它确实有效:

document.getElementById("rect1").style.setProperty("animation-name", "blinking-effect");
document.getElementById("rect1").style.setProperty("animation-duration", "0.5s");
document.getElementById("rect1").style.setProperty("animation-iteration-count", "infinite");
document.getElementById("rect1").style.setProperty("animation-timing-function", "ease-in-out");
document.getElementById("rect1").style.setProperty("animation-direction", "alternate");

有没有办法在不使用 JavaScript 的情况下做到这一点?

最佳答案

丢失 CSS 上的引号。

.animation {
    animation-name: blinking-effect;
    animation-duration: 0.5s;
    animation-iteration-count: infinite;
    animation-timing-function: ease-in-out;
    animation-direction: alternate;
}

另外,矩形有重复的 y 属性并且没有宽度,我认为这是一个拼写错误,并且动画类属性以空格结尾,我认为这是另一个拼写错误。

关于javascript - SVG 动画在以编程方式设置样式时有效,但在仅使用样式表时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27495894/

相关文章:

javascript - 何时以及为何使用这些不同的 Javascript 函数声明格式?

javascript - 不使用 .toFixed() 将浮点格式设置为两位小数

html - 我无法更改背景图片大小

javascript - 拖拽旋转界面 : how to make it work on a touchscreen?

javascript - THREE.SVGRenderer 保存图像文本

Javascript、CanvasRenderingContext2D.drawImage 无法识别图像数据

javascript - 如何更改 “Are you sure you want to leave this page?”函数脚本的文本 "onunload"?

html - 如何正确清除这些 float 的 div?

html - AngularJS - 动态更新 ng 类名称

javascript - 如何在不缩放绑定(bind)到时间轴的其他形状的情况下放大/缩小 d3.time.scale