+ 3
Are there any ways to change CSS style dynamically with JS besides this? And is this a good way? 😀
5 Respuestas
+ 6
1. 有的
requestAnimationFrame() 是比 setInterval() 更適合做動畫的,因為會自動按照最佳時機補上下一格。
例子:
https://code.sololearn.com/WZV2FsGPRwr6/?ref=app
詳情請參閱這篇文章:
https://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/
2. 不是好方法
Calvin 說的只用CSS才是好方法。
除非你在編遊戲,動畫與遊戲參數掛勾。
+ 6
Animation that not require any logic to control, is better to use css to control it
https://code.sololearn.com/WU5LZViaGKXY/?ref=app
+ 1
You add stylesheet in js and change it with js!
function test() {
document.getElementById('idname').style.background = 'green' ;
}
+ 1
U can use classlist.add, first make a class with a new style the classlist.add it using js
see link for more info.
https://alligator.io/js/classlist/
+ 1
Gordon 感谢(・ω< )★