Slow JavaScript animation in Chrome

前端 未结 2 1295
逝去的感伤
逝去的感伤 2021-01-06 03:46

i have a problem with the style manipulation of an HTML object in Chrome.

Here is an example:

var a = document.createElement(\'div\');

a.style.posit         


        
2条回答
  •  甜味超标
    2021-01-06 04:14

    First of all, interval timers are not guaranteed to run exactly at the time you set them for, particularly if the time is a very short interval. In many browsers, there is a minimum time value allowed for setInterval(). So, if that minimum time is greater than 13ms or if many of the interval calls come in longer than 13ms, your animation that has a fixed number of steps will take longer and go slower.

    Second of all, any reliable animation must use a tweening algorithm where it calculates the steps it wants to use and then upon each step, it re-evaluates (by comparing the system time with the expected system time for this step number) whether it's behind schedule or ahead of schedule and adjusts the future step size accordingly so that it will complete on time and appear to be going the appropriate speed. That's what jQuery's animation does. That's what your setInterval() animation does not do.

    Here is a reference article on self-adjusting timers for animations that run a predictable amount of time and another article on minimum times for timers.

    Here's an article and code on tweening and another piece of code that does tweening.

    Also, for simplicity's sake, please change this:

    document.getElementsByTagName('body')[0]
    

    to this:

    document.body
    

提交回复
热议问题