当鼠标移动且未悬停在 div 上时淡入内容

发布于 2024-10-12 06:10:22 字数 569 浏览 3 评论 0原文

我试图在鼠标移动时淡入内容(类似于 Google 主页的方式),但是如果鼠标光标悬停在特定 div 上,我想防止这种行为。

基础知识如下:

$("html").hover(function() {
    $("#navigation, #content").fadeIn("slow");
});

这成功地淡入了内容。我一直在尝试检测光标何时位于某个元素上,但没有效果。这就是我所拥有的:

$("html").one("mousemove", function() {
    if (mouseover == false) {
        $("#navigation, #content").fadeIn("slow");
    }
});

var mouseover = false;
$('#hero').mouseenter(function() {
    mouseover = true;
}).mouseleave(function() {
    mouseover = false;
});

可能有比我这里所拥有的更好的方法来完成此任务吗?

I am trying to fade in content when the mouse moves (similar to the way Google's homepage does), however I want to prevent this behaviour if the mouse cursor is hovering over a specific div.

Here's the basics:

$("html").hover(function() {
    $("#navigation, #content").fadeIn("slow");
});

This successfully fades in the content. I have been trying to detect when the cursor is over a certain element but with no effect. This is what I have:

$("html").one("mousemove", function() {
    if (mouseover == false) {
        $("#navigation, #content").fadeIn("slow");
    }
});

var mouseover = false;
$('#hero').mouseenter(function() {
    mouseover = true;
}).mouseleave(function() {
    mouseover = false;
});

There may be a better way to accomplish this than what I have here?

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

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

发布评论

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

评论(3

心不设防 2024-10-19 06:10:22

尝试在 herostopPropagation() 方法> 元素。这应该会阻止事件冒泡到文档级别,该事件将由更通用的函数处理。

请注意,我个人从未这样做过,但我对 javascript 中的事件冒泡以及 stopPropagation() 方法的目的的理解是,它应该完全为您执行此操作。

Try using jQuery's stopPropagation() method on the mousemove handler on your hero element. That should stop the event from bubbling up to the document level, where it would be handled by your more general function.

Note that I've never done this personally, but my understanding of event bubbling in javascript and the purpose of the stopPropagation() method is that it should do exactly this for you.

等往事风中吹 2024-10-19 06:10:22

而不是 $('html') 尝试 $(document) 并且由于此元素是隐藏的,请尝试检测鼠标悬停在该元素上时的 x 和 y,因此如果鼠标的 x 大于或等于该元素的 x 且小于或等于其宽度加上 x,以及 y 的相同逻辑,显示它。

并记住取消事件,因此使用 unbind()。

Rather than $('html') try $(document) and since this element is hidden, try detecting the x and y of the mouse when its over the element, so if the mouse's x is greater than or equal to the element's x and less than or equal to its width plus its x, and the same logic for the y, show it.

And remember to cancel the event, so use unbind().

暗地喜欢 2024-10-19 06:10:22

好的,我可以在 Firefox 中使用此功能:

var overHero = false;
$('#hero').mouseover(function() {
    overHero = true;
}).mouseleave(function(){
    overHero = false;
});

$('html').mousemove( function() {
    if (overHero == false) {
        $("#navigation, #content").fadeIn("slow");
    }
});

...但它在 Chrome 中不起作用。无论鼠标是否移动,Chrome 都会立即淡入 #content。如果鼠标位于 #hero 元素上,但它不会淡入(这是正确的)。

Ok so I have this working in Firefox:

var overHero = false;
$('#hero').mouseover(function() {
    overHero = true;
}).mouseleave(function(){
    overHero = false;
});

$('html').mousemove( function() {
    if (overHero == false) {
        $("#navigation, #content").fadeIn("slow");
    }
});

...but it doesn't work in Chrome. Chrome just fades in the #content straight away regardless of whether the mouse is moving or not. If the mouse is over the #hero element though it does not fade in (which is correct).

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