鼠标悬停/悬停效果在 IE8 上缓慢

发布于 2024-08-06 15:18:43 字数 523 浏览 6 评论 0原文

我注意到在 IE8 中,当在包含许多行(本例中为 100 行)的表上使用鼠标悬停事件时,性能会出现奇怪的情况。我尝试了很多不同的方法,但我似乎找不到任何方法来按照我喜欢/需要的速度获得它。

如果我在每个事件上切换类,则所有 IE 版本的性能都会下降,如果我通过 javascript 直接操作 CSS,IE6 和 IE7 会加速很多,但 IE8 的性能仍然很差。

有什么想法吗?我真的很想知道是什么使得鼠标悬停事件与所有其他浏览器相比执行得如此缓慢。

如果这种情况只发生在IE6上我可以理解并让它过去,但是当最新版本的浏览器是最慢的浏览器时,只会有越来越多的用户体验不好。

使用 JQuery 悬停的示例: http://thedungheap.net/research/

编辑:我现在更新了示例,以便很容易看出 10 行和 200 行之间的区别。这是在同一个文档中,所以我猜这不会是整个 DOM 大小的问题

I have noticed a weird performance thing in IE8 when using mouseover events on a table with many rows (100 in this example). I have tried a lot of different approaches but I can't seem to find any way to get it as fast as I like/need.

If I switch classes on each event the performance goes down in all IE versions, and If I use direct manipulation of the CSS through javascript IE6 and IE7 speeds up alot, but IE8 still performs lousy.

Any ideas ? I would really like to know what it is that makes the mouseover event to perform so sluggish compared to all the other browsers.

If this only happened to IE6 I could understand and let it pass, but when the newest version of the browser is the slowest one, there is only going to be more and more users with a bad experience.

Example using JQuery hover: http://thedungheap.net/research/

EDIT: I have now updated the example so that it is easy to see the difference between having 10 rows and 200. This is in the same document, so this cannot be a problem with the whole DOM size, i guess

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

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

发布评论

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

评论(7

花心好男孩 2024-08-13 15:18:43

:hover 在 IE8 上非常慢,无论您​​打算如何实现它。事实上,javascript onmouseover、onmouseout 事件提供了比 CSS 更快的方法来创建悬停效果

IE8 上最快的示例:

<table>
<tr style="background-color:#FFFFFF" onmouseover="this.style.backgroundColor='#000000'" onmouseout="this.style.backgroundColor='#FFFFFF'">
   <td>foo bar</td>
</tr>
</table>

较慢的示例:

<style type="text/css">
   tr.S1    {background-color:#000000}
   tr.S2    {background-color:#FFFFFF}
</style>
<table>
<tr class="S1" onmouseover="this.className='S2'" onmouseout="this.className='S1'">
   <td>foo bar</td>
</tr>
</table>

非常慢的示例: JSFiddle

<style type="text/css">
   tr.S     {background-color:#000000}
   tr.S:hover   {background-color:#FFFFFF}
</style>
<table>
<tr class="S">
   <td>foo bar</td>
</tr>
</table>

The :hover IS very slow on IE8, no matter how you intend to implement it. In fact, the javascript onmouseover, onmouseout events provides way faster methods for creating a hover effect, than CSS does

Fastest example on IE8:

<table>
<tr style="background-color:#FFFFFF" onmouseover="this.style.backgroundColor='#000000'" onmouseout="this.style.backgroundColor='#FFFFFF'">
   <td>foo bar</td>
</tr>
</table>

Slower example:

<style type="text/css">
   tr.S1    {background-color:#000000}
   tr.S2    {background-color:#FFFFFF}
</style>
<table>
<tr class="S1" onmouseover="this.className='S2'" onmouseout="this.className='S1'">
   <td>foo bar</td>
</tr>
</table>

VERY slow example: JSFiddle

<style type="text/css">
   tr.S     {background-color:#000000}
   tr.S:hover   {background-color:#FFFFFF}
</style>
<table>
<tr class="S">
   <td>foo bar</td>
</tr>
</table>
许你一世情深 2024-08-13 15:18:43

顺便说一句,对于所有浏览器,您都可以使用 :hover 选择器,仅使用 css。并且仅对于 IE6,您可以添加最快的解决方案。

Btw for all browsers you can use :hover selector using css only. And only for IE6 you can add your fastest soluton.

夜声 2024-08-13 15:18:43

尝试使用事件冒泡。仅将悬停事件添加到表格中,然后查看目标元素。

$(function() {
    $('table').hover(function(e) {
        $(e.originalTarget.parentNode).css('backgroundColor', '#ffc000');
    }, function(e) {
        $(e.originalTarget.parentNode).css('backgroundColor', '#fff');
    });
});

Try using event bubbling. Add the hover event to the table only, and then look at the target element.

$(function() {
    $('table').hover(function(e) {
        $(e.originalTarget.parentNode).css('backgroundColor', '#ffc000');
    }, function(e) {
        $(e.originalTarget.parentNode).css('backgroundColor', '#fff');
    });
});
晌融 2024-08-13 15:18:43

您是否尝试过看看如果每行只有一个会发生什么?好奇 DOM [或每行] 中的元素数量是否会影响性能。否则,可能是 ie8 在选择器引擎中遍历标签的方式出现问题。这并不是真正的答案,而是值得尝试的事情。

没有IE8,否则我会自己尝试一下。

Have you tried to see what happens if you only have one per row? Curious if it is the number of elements in the DOM [or in each row] could affect performance. Otherwise, it could be an issue with the way ie8 traverses tags in the selector engine. Not really an answer, but something to try.

No IE8 or I'd try it myself.

天荒地未老 2024-08-13 15:18:43

对我来说似乎足够快,无需实际查看指标。

您可以尝试鼠标悬停/鼠标移出而不是切换。您还可以尝试事件委托,这通常有助于处理 dom 中的众多元素。

    $("tr").mouseover(function() {
            $(this).css('backgroundColor', '#ffc000');
        })
        .mouseout(function() {
            $(this).css('backgroundColor', '#fff');
        });

Seems fast enough to me, without actually looking at metrics.

You could try mouseover/mouseout instead of toggling. You could also try event delegation, which often helps with this many elements in the dom.

    $("tr").mouseover(function() {
            $(this).css('backgroundColor', '#ffc000');
        })
        .mouseout(function() {
            $(this).css('backgroundColor', '#fff');
        });
不…忘初心 2024-08-13 15:18:43

我遇到过这个问题并实施了以下解决方法,

var viewTable = jQuery("table.MyTable");
var temDiv = jQuery("<div class=\"HighlightClass\" style=\"display:none\"></div>").appendTo("body");
var highlightColor = temDiv.css("background-color");
viewTable.mouseover(function(eventObj){
    jQuery(eventObj.target).parents("tr:first").css("background-color", highlightColor);
}).mouseout(function(eventObj){
    jQuery(eventObj.target).parents("tr:first").css("background-color","");
});

希望这对您有用。

I have faced this issue and implemented the following workaround

var viewTable = jQuery("table.MyTable");
var temDiv = jQuery("<div class=\"HighlightClass\" style=\"display:none\"></div>").appendTo("body");
var highlightColor = temDiv.css("background-color");
viewTable.mouseover(function(eventObj){
    jQuery(eventObj.target).parents("tr:first").css("background-color", highlightColor);
}).mouseout(function(eventObj){
    jQuery(eventObj.target).parents("tr:first").css("background-color","");
});

I hope this could be useful for you.

梦言归人 2024-08-13 15:18:43

很抱歉发布这么旧的答案,但我认为它是相关的,并且该页面在谷歌上排名很高,所以...

哇,我刚刚在这个问题上花了很多时间,我试图使用Javascript,但仍然很慢。

如果您使用背景图像,这是一个解决方案:

这对我来说是一个真正的问题,因为我遇到此问题的项目是我用来对选项卡进行动画处理的左右按钮/箭头上的悬停效果向左和向右,选项卡将位于按钮下方,如果我可以说,选项卡幻灯片放映,当光标进入按钮区域时,正常图像将消失,下面的图像将显示几毫秒,然后,悬停图像将最终显示出来,丑陋。

真正的解决方案是使用图像精灵,这样即使在纯 CSS 中也绝对没有延迟。这个想法是拥有一个包含所有不同图像状态的单个图像(正常/悬停/选定/非活动/等),您将图像设置为背景图像,然后只需调整悬停效果的背景位置值即可其他的。

如果您想更好地了解 css sprites: http://css-tricks.com/css-sprites/

Sorry to post on an answer this old but I think it is relevant and this page is well ranked by google so...

Wow, I just spent a great amount of time on this problem, I tried to use Javascript, but it was still slow.

This is a solution if you use background images :

This was a real issue for me, because the project I had this problem on was the hover effect on Left and Right buttons / arrows that I use to animate tabs left and right, the tabs would go under the buttons, a tab slideshow if I may say and when the cursor entered the button area the normal image would disappear, the image below would be visible for a few millisecs and then, the hover image would eventually display, ugly.

The real solution was to use image sprites, that way there is absolutely no lag even in pure css. The idea is to have a single image with all the differents images states insides (normal / hover / selected / inactive / etc), you set the image as background-image, and you just adjust the background-position value for the hover effect and others.

If you want to know better about css sprites : http://css-tricks.com/css-sprites/

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