jQuery animate() and browser performance

后端 未结 7 701
小蘑菇
小蘑菇 2020-12-03 09:48

I\'ve got some elements that I\'m moving across the page very slowly. Essentially, I\'m decreasing the left margin of two images over a span of 40 seconds or so.

Vi

相关标签:
7条回答
  • 2020-12-03 10:08

    I really like the answer posted by Tim, although I needed to get this fix working in a Drupal 6 production environment.

    I have jQuery 1.3.2 installed, through the use of jQuery update module, so there are a few differences between what I'm using and jQuery 1.4 that Tim's fix is designed for.

    Following Tim's instructions got me 90% of the way there, I just had to put my thinking cap on for 10 minutes to come up with this code instead..

    timer values of 25 - 33 seem to work a lot better than 13ms for medium speed animations such as fading background images.

    var timerId;
    
    function now() {
        return (new Date).getTime();
    }
    
    jQuery.fx.prototype.custom = function( from, to, unit ) {
        this.startTime = now();
        this.start = from;
        this.end = to;
        this.unit = unit || this.unit || "px";
        this.now = this.start;
        this.pos = this.state = 0;
    
        var self = this;
        function t( gotoEnd ) {
            return self.step(gotoEnd);
        }
    
        t.elem = this.elem;
    
            if ( t() && jQuery.timers.push(t) && !timerId ) {
        timerId = setInterval(function(){
            var timers = jQuery.timers;
    
            for ( var i = 0; i < timers.length; i++ )
                if ( !timers[i]() )
                    timers.splice(i--, 1);
    
            if ( !timers.length ) {
                clearInterval( timerId );
                timerId = undefined;
            }
        }, 25);
    }
    };
    
    0 讨论(0)
  • 2020-12-03 10:16

    People have mentioned slowing down jQuery's refresh rate. You can override the timer function in jQuery 1.4 with this file (jquery.animation-fix.js):

    function now() {
        return (new Date).getTime();
    }
    jQuery.fx.prototype.custom = function( from, to, unit ) {
        this.startTime = now();
        this.start = from;
        this.end = to;
        this.unit = unit || this.unit || "px";
        this.now = this.start;
        this.pos = this.state = 0;
    
        var self = this;
        function t( gotoEnd ) {
            return self.step(gotoEnd);
        }
    
        t.elem = this.elem;
    
        if ( t() && jQuery.timers.push(t) && !jQuery.fx.prototype.timerId ) {
            //timerId = setInterval(jQuery.fx.tick, 13);
            jQuery.fx.prototype.timerId = setInterval(jQuery.fx.tick, 2050);
        }
    }
    

    So modify the line with this in it

    jQuery.fx.prototype.timerId = setInterval(jQuery.fx.tick, 50);
    

    Change the 50 to whatever interval you want. That is in Miliseconds (ms)

    If you save this code in a different file, you can attach it like this:

    <script src="/js/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="/js/jquery.animation-fix.js" type="text/javascript"></script>
    
    0 讨论(0)
  • 2020-12-03 10:26

    jQuery animate uses the javascript function 'setInterval' to update the obects every few milliseconds. Unfortunately the interval in jQuery is per default '13ms'. Thats 76 updates every second. Way to much for such slow and medium animations.

    The 13ms are hardcoded into jQuery. So you can directly change this value in the jQuery.js, only. If you only have the slow clowds you can go up to 100ms. If you have some faster animations, too, you should set it to 50.

    You can change the parameter for setInterval(); in the function custom. 'jQuery.fx.prototype { ... custom: function() { ... } ... }'

    0 讨论(0)
  • 2020-12-03 10:28

    I know this is an oldish question and Tim provided a great answer, but I just thought I should post an update for anyone looking for a solution to this problem, since there's now a simpler way...

    As of jQuery 1.4.3 you can set the interval jQuery's animate uses directly via the jQuery.fx.interval property. So you can simply do something like:

    jQuery.fx.interval = 50;
    
    0 讨论(0)
  • 2020-12-03 10:29

    Animations involve looping operations, and those will really crunch the CPU no matter what.

    I dont know how easy it is to do with jQuery, but what needs to happen is the animation needs to consume less cycles. Either you make the ani a little more jagged (the display isnt as smooth), the looping needs to be optimized, or reduce the work of the ani.

    40 seconds? isnt that a bit long for an animation? I thought they are sposed to be a little more immediate than that.

    0 讨论(0)
  • 2020-12-03 10:30

    I just watched the performance of animation under Scriptaculous, and found similar CPU spikes: roughly 50% for IE, slightly better performance (16-30%) for Firefox -- both on a DuoCore PC. Since both JQuery and Scriptaculous work by changing the underlying CSS, I think it's safe to say that any Javascript implementation is going to be computationally expensive.

    You may well be stuck going with Flash.

    0 讨论(0)
提交回复
热议问题