javascript - 为什么这个 jQuery .animate 调用这么慢?

标签 javascript jquery jquery-animate

大家好,我有一个非常简单的函数

enableModule : function(moduleName){
        var module = $('div#'+moduleName);
        console.log('enabling '+moduleName);
        console.time('animate');
        module.animate({'opacity' : '1.0'}, 300, function(){console.timeEnd('animate');});
        module.find('.disabled_sheild').remove();
        module.removeClass('disabled');
        console.log('end of enable Module');
    }

动画本身,不透明度的变化非常快,但调用它时会有延迟。 console.time() 报告的时间为 2540MS 及以上。我在想这可能是因为 div#module 和它的 child 一起被动画化了吗?但这个剂量有意义,因为我有另一个函数“disableModule”,它反向执行相同的操作并以合理的速度运行。

这里是禁用模块功能,进行的更多,但返回时间约为 242 毫秒

disableModule : function(moduleName){
      $('div#'+moduleName+', div.'+moduleName).each(function(){
        var module = $(this);
        module.prepend('<div class="disabled_sheild"></div>');
        var sheild = module.find('.disabled_sheild');
        sheild.css({'position' : 'absolute', 'z-index' : '200'});
        sheild.width(module.width());
        sheild.height(module.height());
        jQuery.each(jQuery.browser, function(i) {
            if($.browser.msie){
               //module.css("display","none");
               //if using ie give sheild a transparent background layout
            }else{
              console.time('animate');
              module.animate({'opacity' : '0.5'}, function(){ console.timeEnd('animate');});
            }
          });
      });
    }

最佳答案

经过一些艰苦的故障排除后,我发现它是禁用方法中浏览器检测循环的问题:

  jQuery.each(jQuery.browser, function(i) {
      if($.browser.msie){
         //module.css("display","none");
         //if using ie give sheild a transparent background layout
      }else{
        console.time('animate');
        module.animate({opacity : 0.5}, 200, function(){console.timeEnd('animate');});
      }
    });

注释掉这个 block 使一切都加快了速度。在尝试优化其他所有内容后,我差点把头发拔出来。

关于javascript - 为什么这个 jQuery .animate 调用这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1801018/

相关文章:

javascript - 获取在 Python 中使用 Javascript 生成的页面

javascript - 新用户注册后如何显示 "New account is created"消息

javascript - 将 sendAsBinary 与 ASP.Net MVC 结合使用

javascript - 为什么 jQuery 将自己包装在一个函数中?

jquery - 移动面板并调整其大小以模拟生长效果

javascript - 自定义 AngularJS 指令不起作用

javascript - 如何保持响应式移动菜单处于事件状态?

javascript - jQuery 隐藏值未发送

jQuery - 如何将字符串作为参数传递给动画函数

javascript - 在 jquery 中每次单击都会启动新动画