反复添加/删除类会减慢速度吗?

发布于 2024-10-29 09:40:58 字数 565 浏览 1 评论 0原文

所以我想用 jQuery 在滚动上做一些事情...

$(window).scroll(function() {
  //so it's not overly aggressive calling the funciton
  setTimeout(function(){check_scroll()}, 50);
}); 

然后...

function check_scroll(){
  var scroll = $(window).scrollTop()
  if(scroll > 100) {
    $("#fixed").addClass("fixed");
  }
  else
    $("#fixed").removeClass("fixed");  
};

所以,我的问题是,#fixed 大多数时候都会有类“.fixed”,所以如果我反复要求 jQuery 添加它,会不会表现不佳?我应该首先检查它是否有该类,然后尝试添加它吗?

现在看起来不错,但我的应用程序将会增长,并且将会有大量的 js 运行,所以我想充分利用每一点可能的性能。

So I want to do a couple of things on scroll with jQuery...

$(window).scroll(function() {
  //so it's not overly aggressive calling the funciton
  setTimeout(function(){check_scroll()}, 50);
}); 

And then...

function check_scroll(){
  var scroll = $(window).scrollTop()
  if(scroll > 100) {
    $("#fixed").addClass("fixed");
  }
  else
    $("#fixed").removeClass("fixed");  
};

So, my question is, #fixed will have the class ".fixed" most of the time, so if I repeatedly ask jQuery to add it, will it be non-performant? Should I first check if it has the class and then try to add it?

It seems fine as it is now, but my app will grow and there will be a lot of js going on, so I want to milk every bit of performance possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最偏执的依靠 2024-11-05 09:40:58

如果您真的担心性能,那么 $('#fixed') 将会更值得关注。您应该将结果保存在一个变量中,除非 #fixed 每次查询时都会有所不同。

添加和删​​除类的问题主要是“这取决于”。添加和删​​除类实际上有什么作用?它是否会改变页面的布局,或者您是否将其用作其他内容的标志?如果它改变了页面的布局,那么你就别无选择。如果没有,那么您可以使用 .data 方法来存储状态,这将避免 回流(添加/删除类的主要性能问题)。

我还关心你的 check_scroll 函数在实践中会被调用多少次。您不会取消以前的 setTimout 事件,因此,您最终会收到一堆 setTimeout 调用。也许你想要更多类似的东西:

(function() {
  var timeout = null, fixed = $('#fixed'), win=$(window),
   check_scroll = function() {
      var scroll = win.scrollTop();
      fixed.toggleClass("fixed", scroll > 100);
      timeout=null;
   }
   win.scroll(function() {
      if (timeout) {
         // only do once every 50ms
         return;
      }
      timeout = setTimeout(check_scroll, 50);
   });

If you are really worried about performance, the $('#fixed') is going to be of greater concern. You should stick the result of that in a variable unless #fixed is going to be something different every-time you query for it.

The question of adding and removing a class is mostly, "it depends". What does adding and removing the class actually do? Does it change the layout of the page or are you using it as a flag for something else? If it changes the layout of the page and then you have no choice. If it doesn't, then you could use the .data method instead to store state which will avoid reflow (the major performance issue with adding/removing classes).

I am also concerned about how many times your check_scroll function is going to be called in practice. You are not cancelling previous setTimout events and so, you will end up with a bunch of setTimeout calls. Probably you want something more like:

(function() {
  var timeout = null, fixed = $('#fixed'), win=$(window),
   check_scroll = function() {
      var scroll = win.scrollTop();
      fixed.toggleClass("fixed", scroll > 100);
      timeout=null;
   }
   win.scroll(function() {
      if (timeout) {
         // only do once every 50ms
         return;
      }
      timeout = setTimeout(check_scroll, 50);
   });
半世蒼涼 2024-11-05 09:40:58

我认为使用元素上已存在的类调用 addClass 不会对性能造成影响。

您可以使用这段代码,它应该表现良好,因为在调用 addClass 或 removeClass 之前,toggleClass 首先使用 hasClass:

$("#fixed").toggleClass("fixed", scroll > 100);

I don't think you will take a performance hit for calling addClass with a class that already exists on the element.

You could use this block of code and it should perform well since toggleClass first uses hasClass before calling addClass or removeClass:

$("#fixed").toggleClass("fixed", scroll > 100);
蹲墙角沉默 2024-11-05 09:40:58

这段代码应该没问题,

jquery 文件只在浏览器中加载一次,一旦加载就可以完全运行。浏览器不需要每次调用文件来执行命令时都重新加载它。

在我看来,你的代码应该没问题。

This code should be fine,

The jquery file is only loaded once in a browser and once it is loaded it is fully operational. The browser doesn't need to reload it everytime it calls to the file to execute a command.

Your code should be fine, in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文