使用jquery addClass和removeClass时浏览器重画非常慢
我有一个页面需要动态添加 css 类。我的页面中有以下 Jquery 代码,
myElementsList.addClass('AClass').removeClass('BClass')
这些 css 类将更改我的元素的颜色和背景颜色。问题是重新绘制浏览器需要 2 或 3 秒的时间。
如果我使用不存在(或不重新绘制浏览器)的CSS类,那么它将很快执行。
myElementsList.addClass('NotEXistClassA').removeClass('NotEXistClassB')
有什么建议会受到欢迎吗?
编辑:
我解决这个问题的方法是先更改前 20 行,然后使用计时器更改其余行。如果事件在计时器到期之前再次引发,我也会重置此计时器。
欢迎任何其他建议。
I have a page where I need to dynamically add or css classes. I have the following Jquery code in my page,
myElementsList.addClass('AClass').removeClass('BClass')
These css classes will change the color and background color of my elements. The problem is that this takes 2 or 3 seconds to repaint the browser.
If i use css classes which does not eXist(or does not repaint the browser) then it will eecute very quickly.
myElementsList.addClass('NotEXistClassA').removeClass('NotEXistClassB')
Any suggestion will be welcome?
Edit:
The way I solve this issue by changing the first 20 rows first and changing the remaining using a timer. I am also reseting this timer every time if the events raised again before the timer elapased.
Any other suggestion is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是你试图让浏览器同时做两件事,这两者都需要它重新绘制相同的东西。
但事实上,您只需执行这两个操作之一即可实现您想要做的事情(更改行的颜色)。
您需要进行的基本更改不是为“未选择”设置一种样式,为“选择”设置另一种样式,而是为“默认”设置一种样式,为“选择”设置一种样式。
然后,您可以使用“默认”样式来设置标准颜色,然后只需添加“选定”样式即可覆盖它;您不需要删除默认样式,因为所选样式将覆盖它。
下面是一些简单的 CSS 代码,可以帮助您入门:
...当您选择它时,脚本将执行
addClass('selected')
操作,然后removeClass('selected')
code> 当您取消选择它时。实际上根本不需要
not-selected
类。这个简单的更改将消除您的程序在切换选择时正在执行的工作的一半,事实上,它很可能将速度提高 50% 以上,因为它不必在对象上进行多次重新绘制。相同的元素。
这肯定会让你走得更快一些。这并不是页面速度缓慢的全部原因,但它肯定会有很大帮助。
The problem here is that you are trying to get the browser to do two things at once, which both require it to repaint the same things.
But in fact you can achieve what you want to do (change the colour of the rows) by only doing one of the two actions.
The basic change you need to make is not to have a style for "not-selected" and another style for "selected", but instead to have one for "default" and one for "selected".
Then you can have the "default" style to set the standard colour, and simply add the "selected" style to override it; you don't need to remove the default style, as the selected one will override it.
Here's a simple bit of CSS to get you started:
...and the script would just do
addClass('selected')
when you select it, andremoveClass('selected')
when you deselect it.There really is no need for a
not-selected
class at all.That simple change will remove a full half of the work that your program is doing when you toggle the selection, and in fact it will quite possibly speed it up by more an 50% due to it not having to do multiple re-paints on the same elements.
That will certainly get you going a bit faster. It isn't the whole story as to why your page is slow, but it will certainly help a lot.
首先删除班级,然后分配新班级,
我认为这可能对您有帮助。
First remove class then assign new class
I think this may help you.