简体   繁体   English

如何更改 Javascript 中关键帧的 CSS 属性?

[英]How to change a CSS attribute of keyframes in Javascript?

I have 2 selectors from and to inside of @keyframes scope like this:我在@keyframes scope 内部有两个选择from to所示:

@keyframes pie {
    from {
        stroke-dashoffset: 0;
    }
    to {
        stroke-dashoffset: 77px;
    }
}

I would like to change to 's attribute through JavaScript, but seems like giving style property to .pie doesn't work as I wanted it.我想通过 JavaScript 更改to ' 属性,但似乎将style属性赋予.pie并不能如我所愿。 It creates the attribute to .pie directly instead of to selector.它直接将属性创建到.pie而不是to器。

This is how I've done so far:这就是我到目前为止所做的:

 class Timer { constructor(elem) { this.elem = document.querySelector(elem); this.change(); } change() { this.elem.style.strokeDashoffset = 10 + 'px'; } } let getTimer = new Timer('.pie');
 svg.timer { width: 40px; height: 40px; transform: rotateY(-180deg) rotateZ(-90deg); } circle { stroke-dasharray: 77px; stroke-dashoffset: 0px; stroke-width: 2px; stroke: brown; animation: pie 10s linear infinite forwards; } @keyframes pie { from { stroke-dashoffset: 0; } to { stroke-dashoffset: 77px; } }
 <div class="pie"> <svg class="timer"> <circle r="12" cx="20" cy="20"> </circle> </svg> </div>

Are there any ways to change the attribute inside of to selector through Javascript?有没有办法通过 Javascript to选择器内部的attribute

The easiest is to use CSS-variables :最简单的是使用CSS-variables

 class Timer { constructor(elem) { this.elem = document.querySelector(elem); this.change(); } change() { this.elem.style.setProperty('--dashoffset', 10 + 'px'); } } let getTimer = new Timer('.pie');
 svg.timer { width: 40px; height: 40px; transform: rotateY(-180deg) rotateZ(-90deg); } circle { stroke-dasharray: 77px; stroke-dashoffset: 0px; stroke-width: 2px; stroke: brown; animation: pie 10s linear infinite forwards; } @keyframes pie { from { stroke-dashoffset: 0; } to { stroke-dashoffset: 77px; stroke-dashoffset: var(--dashoffset); } }.pie { --dashoffset: 77px; }
 <div class="pie"> <svg class="timer"> <circle r="12" cx="20" cy="20"> </circle> </svg> </div>

However its support is still not that great, and it's really hard to polyfill so if you have to handle legacy browsers, you may need to rewrite the rule directly, by appending a new <style> with the whole @keyframes block:然而,它的支持仍然不是那么好,而且很难进行 polyfill,所以如果你必须处理旧版浏览器,你可能需要直接重写规则,通过在整个 @keyframes 块中附加一个新的 <style> :

 class Timer { constructor(elem) { this.elem = document.querySelector(elem); this.change(); } change() { const style = document.createElement('style'); style.textContent = ` @keyframes pie { from { stroke-dashoffset: 0; } to { stroke-dashoffset: ${ 10 }px; } }` document.head.appendChild( style ); } } let getTimer = new Timer('.pie');
 svg.timer { width: 40px; height: 40px; transform: rotateY(-180deg) rotateZ(-90deg); } circle { stroke-dasharray: 77px; stroke-dashoffset: 0px; stroke-width: 2px; stroke: brown; animation: pie 10s linear infinite forwards; } @keyframes pie { from { stroke-dashoffset: 0; } to { stroke-dashoffset: 77px; } }
 <div class="pie"> <svg class="timer"> <circle r="12" cx="20" cy="20"> </circle> </svg> </div>

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

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