jQuery 鼠标移动性能
当我将 mousemove 事件绑定到元素时,它可以在除 Internet Explorer 之外的所有浏览器中顺利运行。对于 IE,CPU 使用率过高,并且一些相关的东西(例如工具提示)很丑陋。有什么办法可以解决性能问题吗? (是的,我知道,不要使用 IE :))
更新:即使我在事件处理函数中不执行任何操作,CPU 使用率仍然很高。这是我的代码:
$("#container").live("mousemove", function(e){
});
正常吗?
When I bind a mousemove event to an element it is working smoothly with every browser except Internet Explorer. With IE the CPU usage is way too much and some associated things (eg. tooltip) are ugly. Is there any way I could rid of the performance problem? (yeah I know, don't use IE :))
UPDATE: Even if I don't do anything in the event handler function, the CPU usage is still high. Here's my code:
$("#container").live("mousemove", function(e){
});
Is it normal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在 mousemove 事件中使用 jquery 选择器吗?我见过 jquery 选择器在复杂页面中变慢的情况,如果将选择器放入多次触发的事件中,则会出现明显的滞后。在许多情况下,您可以在 mousemove 之前存储对元素的 jquery 引用,然后 mousemove 使用该元素引用,而不是再次使用每次调用时在内部重新遍历 DOM 的选择器。
Are you using jquery selectors in the mousemove event? I have seen cases where the jquery selectors slow down in complex pages, if you put the selector in an event that fires many times, there is noticeable lag. In many cases you can just store the jquery reference to the element before mousemove, then the mousemove uses the element reference instead of again using a selector that internally re-traverses the DOM every time it is called.
即使在 IE 中,将简单更新绑定到鼠标移动事件也不会有任何问题。正如最近在无数网站中看到的那样,拖放正是通过这种方式实现的。
如果您看到 CPU 出现大幅峰值,我认为可能有更大的根本原因。
You should have no problems binding simple updates to the mouse move event, even in IE. Drag/drop, as seen in a gazillion websites recently, is implemented exactly this way.
If you're seeing massive spikes in CPU, I'd consider perhaps there's a larger underlying cause.