简体   繁体   English

Internet Explorer的兄弟选择器非常慢:悬停

[英]Internet Explorer's sibling selectors are terribly slow with :hover

Example page: http://jsfiddle.net/y2pwqch6/6/ 示例页面: http//jsfiddle.net/y2pwqch6/6/

The example has 1 CSS selector, which is adjacent sibling selector: 该示例有1个CSS选择器,它与兄弟选择器相邻:

.l:hover + .r {
    color: blue
}

The problem is hovering over any element on the page makes Internet Explorer (up to the latest 11 on Win 8.1) recompute too many styles. 问题是悬停在页面上的任何元素上使得Internet Explorer(在Win 8.1上最新的11个)重新计算太多样式。 To reproduce, head over to the example page, and move your mouse in the white results area. 要重现,请转到示例页面,然后在白色结果区域中移动鼠标。 You can draw circles or just move the mouse vertically, you don't have to touch letters. 您可以绘制圆圈或只是垂直移动鼠标,您不必触摸字母。 This makes IE occupy a single CPU core for 100%; 这使得IE占据100%的单CPU核心; the CPU load for Firefox and Chrome in this example is literally 0. 在这个例子中,Firefox和Chrome的CPU负载实际上是0。

If the example looks fine for you, try doubling the amount of elements in case your CPU is fast, or add this CSS to will visualize the slow style updates: 如果该示例对您来说很好,请尝试在CPU快速时将元素数量加倍,或者添加此CSS以显示慢速样式更新:

div:hover { background: gray }

Things that fix the problem: 解决问题的方法:

  • Removing :hover 删除:悬停

Things that don't fix the problem: 无法解决问题的事情:

  • Switching to general sibling selector (~) 切换到通用兄弟选择器(〜)

Any workarounds for this? 有没有解决方法呢? I'm thinking of some global javascript that will add classes to hovered elements. 我正在考虑一些全局的javascript,它会为悬停的元素添加类。

Workaround using javascript: 使用javascript解决方法:

;((() => {
    const selector = '.l';
    $('body').delegate(selector, 'mouseover', e => {
        $(e.target).addClass('GlobalHovered');
    });
    $('body').delegate(selector, 'mouseout', e => {
        $(e.target).removeClass('GlobalHovered');
    });
})());

Replace :hover with .GlobalHovered , and the .l selector with what you need. 替换:hover使用.GlobalHovered :hover ,并使用.l选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM