jQuery Mousemove 和性能问题

发布于 2024-12-06 18:08:11 字数 971 浏览 2 评论 0原文

我刚刚编写了一些代码(并且它有效),用于当鼠标位于 4 个矩形中的任何一个上时在鼠标附近显示一些文本(不同的矩形有不同的文本)。我使用了 html 标签 <地图>> /map>、css 和 jquery。一切正常。我不喜欢当鼠标在图片上移动时 CPU 使用率达到 100%。

这是一个jquery部分:

$('area').each(function(){
    var area = $(this),
        alt = area.attr('alt');
    area.mousemove(function(event){
        var tPosX = event.pageX +10; 
        var tPosY = event.pageY - 85;
        $('#rectangletext').css({top: tPosY, left: tPosX});
        $('#rectangletext').html(alt);
    }).mouseleave(function(){
        $('#rectangletext').html('');
    });
});

我已经在IE、FF、Chrome和Opera中测试了它(同时,在同一台计算机上)。当您将鼠标移动到该区域时,Area.mousemove 会占用高达 100% 的 CPU。问题是:如何减少在地图上移动鼠标时所需的资源? IE 是最差的 - CPU 使用率跃升至 100%。 FF大概吃掉了67%-100%。 Opera 的进食率低于 62%(从不超过 62%)。 Chrome 是最好的:平均约为 28%,最高为 42%。

如果有助于减少所需的资源,可以每隔 300 毫秒将文本重新定位到鼠标附近,而不是每毫秒一次。怎么做呢? 对于这个问题,除了使用 mouseenter 而不是 mousemove 之外,还有什么更好的解决方案吗? mouseenter 的缺点是在调用 mouseleave 之前它不会更新弹出文本的位置。

谢谢。

I've just wrote some code (and it works) for displaying some text near the mouse when the mouse is on any of 4 rectangles (different text for different rectangle). I used html tag < map >< /map>, css and jquery. Everything works fine. I don't like 100% CPU Usage when mouse is moving on the picture.

This is a jquery part:

$('area').each(function(){
    var area = $(this),
        alt = area.attr('alt');
    area.mousemove(function(event){
        var tPosX = event.pageX +10; 
        var tPosY = event.pageY - 85;
        $('#rectangletext').css({top: tPosY, left: tPosX});
        $('#rectangletext').html(alt);
    }).mouseleave(function(){
        $('#rectangletext').html('');
    });
});

I've tested it in IE, FF, Chrome and Opera (at the same time, on the same computer). Area.mousemove eats up to 100% CPU when you move your mouse on that . The question is: how to reduce resources that are needed when you move your mouse on that map? IE is the worst - CPU Usage jumps up to 100%. FF eats about 67%-100%. Opera eats less than 62% (never more than 62%). Chrome is the best: average is about 28%, maximum is 42%.

It's OK to reposition text to be near the mouse not every millisecond, but every 300 milliseconds, if it helps to reduce the resources that are required. How to do that?
Any better solution for this problem than to use mouseenter instead of mousemove? The cons of mouseenter is it doesn't update the position of the popup text until mouseleave is called.

Thank you.

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

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

发布评论

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

评论(3

阳光下的泡沫是彩色的 2024-12-13 18:08:11

您可以跟踪鼠标上次移动的时间。

var prevDate; // keep this as a global variable 

// put the following inside the mousemove function
var date = new Date().getTime();
if(date - prevDate > 300){
    // your code goes here
    prevDate = date;
}

You can keep track of the time your mouse last moved.

var prevDate; // keep this as a global variable 

// put the following inside the mousemove function
var date = new Date().getTime();
if(date - prevDate > 300){
    // your code goes here
    prevDate = date;
}
千と千尋 2024-12-13 18:08:11

您可以在 mouseenter 上开始一个间隔并更新其中的位置。
调整间隔时间以找到合适的频率。
另外,将 jquery 对象存储在变量中可能会有所帮助,但效果不大,因为您通过 ID 访问它们,速度相当快。

You could start an interval on mouseenter and update the position in there.
Play around with the interval time to find a good frequency.
Also storing the jquery objects in a variable could help a bit, but not much since you're accessing them via ID which is pretty fast.

赏烟花じ飞满天 2024-12-13 18:08:11

设置 html 的成本相当高,您只需要在 mouseenter 上完成即可。将选择器移到循环之外也会给你带来很好的加速。

var $rectText = $("#rectangletext");
$('area').each(function(){
    var area = $(this),
        alt = area.attr('alt');
    area.mousemove(function(event){
        var tPosX = event.pageX +10; 
        var tPosY = event.pageY - 85;
        $rectText.css({top: tPosY, left: tPosX});
    }).mouseenter(function(){
        $rectText.html(alt);
    }).mouseleave(function(){
        $rectText.html('');
    });
});

Setting the html is pretty expensive, and you only really need to do it on mouseenter. Moving your selectors outside of the loop will also give you a nice speedup.

var $rectText = $("#rectangletext");
$('area').each(function(){
    var area = $(this),
        alt = area.attr('alt');
    area.mousemove(function(event){
        var tPosX = event.pageX +10; 
        var tPosY = event.pageY - 85;
        $rectText.css({top: tPosY, left: tPosX});
    }).mouseenter(function(){
        $rectText.html(alt);
    }).mouseleave(function(){
        $rectText.html('');
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文