Why :hover is so slow in webkit browsers over large numbers of DOM elements

笑着哭i 提交于 2020-05-12 20:32:49

问题


When there are numerous DOM loaded with javascript elements styled with :hover pseudo class (like a long table), the effect is rendered slow/laggy in Chrome & Safari. Firefox appears to handle the large number of rows with :hover fairly quickly compared with webkit.

For example, you can see the difference by generating 10,000 rows with a :hover effect. http://jsfiddle.net/jschin/VwmjT/

var table = document.createElement('table');

for (var i=0; i<10000; i++) {
    var r = document.createElement('tr');

    for (var j=0; j<3; j++) {
        var c = document.createElement('td');
        c.appendChild(document.createTextNode(i*j));
        r.appendChild(c);
    }

    table.appendChild(r);
}

document.getElementById('b').appendChild(table);

I know 10,000 rows is a bad idea. I know you should paginate. I am just curious as to the nature of why this is.


回答1:


Seems like a bug in Webkit. Actually the bug was caused by this rule:

tr:nth-child(even) {
    background-color: #F8F9FC;
}

if you try to remove it Chrome surprisingly accelerates.

So i updated your fiddle http://jsfiddle.net/m3f4D/

UPDATE: It is a reported bug https://code.google.com/p/chromium/issues/detail?id=160212



来源:https://stackoverflow.com/questions/17197846/why-hover-is-so-slow-in-webkit-browsers-over-large-numbers-of-dom-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!