如何在鼠标停止时显示气球工具提示

发布于 2024-07-07 07:35:36 字数 1136 浏览 8 评论 0原文

[编辑] 所以我使用了下面建议的 JavaScript 工具提示之一。 我得到了一些提示,当你停下来时会显示出来,当你移动时会隐藏起来。 唯一的问题是当我这样做时它可以工作:

document.onmousemove = (function() {
    var onmousestop = function() {
        Tip('Click to search here');
        document.getElementById('MyDiv').onmousemove = function() {
        UnTip();
    };
    }, thread;

    return function() {
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

但我希望该函数仅适用于特定的 div 并且如果我将第一行更改为 "document.getElementById('MyDiv').onmousemove = (function() {" I收到 javascript 错误 document.getElementById('MyDiv') is null 我缺少什么...??

[/edit]

当用户鼠标停留在某个元素上超过 1.5 秒时,我想显示气球样式消息 ,如果他们移动鼠标,我想隐藏气球,我会尝试使用我在野外发现的一些 JavaScript 代码,这是我用来检测鼠标何时停止的代码。

document.onmousemove = (function() {
    var onmousestop = function() {
        //code to show the ballon
        };
    }, thread;

    return function() {
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

然后 有两个问题,一,有人有一个推荐的轻量级 JavaScript 气球吗,它会显示在光标位置;二,检测鼠标停止的代码工作正常,但我对如何检测鼠标再次开始移动并隐藏气球感到困惑。 。 谢谢...

[edit]
So I used one of the javascript tooltips suggested below. I got the tips to show when you stop and hide if you move. The only problem is it works when I do this:

document.onmousemove = (function() {
    var onmousestop = function() {
        Tip('Click to search here');
        document.getElementById('MyDiv').onmousemove = function() {
        UnTip();
    };
    }, thread;

    return function() {
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

But I want the function to only apply to a specific div and if I change the first line to "document.getElementById('MyDiv').onmousemove = (function() {" I get a javascript error document.getElementById('MyDiv') is null What am I missing....??

[/edit]

I want to display a balloon style message when the users mouse stops on an element from more than say 1.5 seconds. And then if they move the mouse I would like to hide the balloon. I am trying to use some JavaScript code I found posted out in the wild. Here is the code I am using to detect when the mouse has stopped:

document.onmousemove = (function() {
    var onmousestop = function() {
        //code to show the ballon
        };
    }, thread;

    return function() {
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

So I have two questions. One, does anyone have a recommended lightweight javascript balloon that will display at the cursor location. And two, the detect mouse stopped code works ok but I am stumped on how to detect that the mouse has started moving again and hide the balloon. Thanks...

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

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

发布评论

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

评论(4

北渚 2024-07-14 07:35:36

回答这个问题有点晚了,但这对有需要的人会有帮助。

我需要此函数能够检测鼠标何时停止移动一段时间,以便在将鼠标悬停在视频上时隐藏 HTML/JS 播放器控制器。 这是工具提示的修订代码:

document.getElementById('MyDiv').onmousemove = (function() {
    var onmousestop = function() {
        Tip('Click to search here');
    }, thread;

    return function() {
        UnTip();
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

在我的例子中,我使用了一些 jQuery 来为我的播放器控制器选择元素:

$('div.video')[0].onmousemove = (function() {
    var onmousestop = function() {
        $('div.controls').fadeOut('fast');
    }, thread;

    return function() {
        $('div.controls').fadeIn('fast');
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

A bit late to be answering this, but this will be helpful for those in need.

I needed this function to be able to detect when the mouse stopped moving for a certain time to hide an HTML/JS player controller when hovering over a video. This is the revised code for the tooltip:

document.getElementById('MyDiv').onmousemove = (function() {
    var onmousestop = function() {
        Tip('Click to search here');
    }, thread;

    return function() {
        UnTip();
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

In my case, I used a bit of jQuery for selecting the elements for my player controller:

$('div.video')[0].onmousemove = (function() {
    var onmousestop = function() {
        $('div.controls').fadeOut('fast');
    }, thread;

    return function() {
        $('div.controls').fadeIn('fast');
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();
随心而道 2024-07-14 07:35:36

jQuery 插件 hoverIntent 提供了类似的行为。 它通过检查用户是否减慢鼠标移入元素并在元素上悬停一定时间来确定用户是否“打算”悬停在特定元素上。

它仅在用户离开元素时触发“out”事件,这听起来并不完全像您正在寻找的那样,但代码非常简单。

当您不需要收集事件时,还要注意将事件绑定到 mousemove,mousemove 会快速触发大量事件,并且可能会对您的站点性能产生严重影响。 hrefIntent 仅在光标进入活动元素时绑定 mousemove,然后解除绑定。

如果你尝试hoverIntent,我在缩小版本不触发“out”事件方面遇到了一些麻烦,所以我建议使用完整的、未缩小的源代码。

The jQuery plugin hoverIntent provides a similar behaviour. It determines if the user 'meant' to hover over a particular element by checking if they slow the mouse down moving into the elements and spend a certain amount of time hovering over the element.

It only fires the "out" event when the user leaves the element, which doesn't sound like exactly what you're looking for, but the code is pretty simple.

Also watch out for binding things to mousemove when you don't need to be collecting the events, mousemove fires a lot of events quickly and can have serious effects on your site performance. hoverIntent only binds mousemove when the cursor enters the active element, and unbinds it afterwards.

If you do try hoverIntent I have had some trouble with the minified version not firing "out" events, so I would recommend using the full, unminified source.

最丧也最甜 2024-07-14 07:35:36

这是一个漂亮的 jQuery 插件,用于提供漂亮的浮动工具提示。

http://jqueryfordesigners.com/demo/coda-bubble.html

[编辑]
我想如果没有看到配套的 HTML,很难说出了什么问题。 我会仔细检查该元素是否具有标记中指定的适当 ID。 除此之外,除非这是一项学术练习,否则我建议使用类似我上面提到的 jQuery 插件之类的东西。 当然还有许多其他类似的预构建工具已经可以处理您当前正在解决的所有细节。

Here's a nifty jQuery plugin for a nice float over tool tip.

http://jqueryfordesigners.com/demo/coda-bubble.html

[edit]
I guess without seeing the companion HTML it's hard to say what's wrong. I'd double check that the element has the appropriate ID specified in the tag. Apart from that, unless this is an academic exercise, I would suggest using something like the jQuery plugin that I referenced above. There are certainly many other pre-built tools like that which will have already dealt with all of the minutiae you're currently addressing.

最初的梦 2024-07-14 07:35:36
document.onmousemove = (function() {
  if($('balloon').visible) {
  //mouse is moving again
}....//your code follows

使用 Prototype.js 语法,一旦气球可见,您就可以确定鼠标已移动。

document.onmousemove = (function() {
  if($('balloon').visible) {
  //mouse is moving again
}....//your code follows

Using Prototype.js syntax you can determine that the mouse has moved once the balloon is visible.

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