悬停时的 JQuery 获取鼠标移入的元素

发布于 2024-08-10 05:18:40 字数 1108 浏览 10 评论 0原文

我有一个导航系统,其中某些元素有单级下拉菜单。我已经让它按照我想要的方式工作,除了当鼠标沿着导航栏移动时很容易掉出导航栏的底部(这是第二条全宽线,带有内联项目)。

我需要的是一种方法,当你从底部掉下来时阻止导航消失,而不干扰其他导航项目(这样当你将鼠标悬停在它们上方时,这些项目仍然会立即隐藏导航)。

有没有办法在调用悬停事件时找到您将鼠标移入的元素?这样我就可以检测到他们是否将鼠标悬停在主体上,并启动计时器以 1000 毫秒左右的时间隐藏导航。

我已经尝试过hoverIntent,但这不能满足我的需要,因为我无法承受从一个导航项移动到另一个导航项的延迟......这使得导航非常难以使用!

或者有没有办法使用鼠标位置检测它们是否只是从导航底部掉出来?

任何帮助将不胜感激。当前导航的 Jquery 如下。

var navDisplayTimer;
var navDisplayObject;
$("#main-nav > li").addClass("js-enabled");
$("#main-nav > li").hover(function(){
 $(this).addClass("hovered");
 if ($(this).find("ul").length != 0) {
  $(this).parent().stop().animate({borderWidth: "22px"}, 400);
  if($(this).parent().css("borderWidth") == "22px 22px 22px 22px") {
   $(this).find("ul").show();
  } else {
   navDisplayObject = this;
   navDisplayTimer = setTimeout(function() {
    $(navDisplayObject).find("ul").show();
   }, 300);
  }
 }
},function(){
 clearTimeout(navDisplayTimer);
 $(this).find("ul").hide();
 $(this).parent().stop().animate({borderWidth: "2px"}, 400);
 $(this).removeClass("hovered");
});

I have a navigation system that has a single level drop down on some of the elements. I have got it working just the way I want, except that it's too easy to just fall out the bottom of the nav when mousing along it (it's a second full width line drop down with inline items).

What I need is a way to stop the nav from dissapearing when you fall out the bottom, without interfering with the other navigation items (so that these still instantly hide the nav when you hover over them).

Is there a way to find the element that you're mousing into when the hover out event is called? This way I could detect if they were mousing over the body and start a timer to hide the nav in a 1000ms or something.

I have tried hoverIntent but this doesn't work for what I need, as I can't afford a delay on moving from one nav item to the other... it makes the nav very difficult to use!

Either that or is there a way to detect if they have simply fallen out the bottom of the nav using mouse position?

Any help would be very appreciated. The Jquery for the current nav is below.

var navDisplayTimer;
var navDisplayObject;
$("#main-nav > li").addClass("js-enabled");
$("#main-nav > li").hover(function(){
 $(this).addClass("hovered");
 if ($(this).find("ul").length != 0) {
  $(this).parent().stop().animate({borderWidth: "22px"}, 400);
  if($(this).parent().css("borderWidth") == "22px 22px 22px 22px") {
   $(this).find("ul").show();
  } else {
   navDisplayObject = this;
   navDisplayTimer = setTimeout(function() {
    $(navDisplayObject).find("ul").show();
   }, 300);
  }
 }
},function(){
 clearTimeout(navDisplayTimer);
 $(this).find("ul").hide();
 $(this).parent().stop().animate({borderWidth: "2px"}, 400);
 $(this).removeClass("hovered");
});

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

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

发布评论

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

评论(1

偏爱自由 2024-08-17 05:18:40

与当前可行的方法略有相反的是,当用户移出菜单 div 时触发事件,但使用超时来执行此操作。

除此之外,当您将鼠标悬停在菜单 div 中的导航项上时,会触发一个事件以清除关闭超时。

这实际上会给用户提供将鼠标重新插入的机会。尝试一下,看看您的想法。

$('#selectednavigationdiv').bind("mouseout", close);
$('#selectednavigationitems').bind("mouseover", function() { clearTimeout(hideTimer ); });// stop drop down menu from being closed

var hideTimer = setTimeout(function() {  }, 1);//initialise so not undefined
function close() {
    clearTimeout(hideTimer );
    hideTimer = setTimeout(function() { $('#selectednavigationdiv').slideUp('fast') }, 1000);//close drop down menu                    
}

A slight inverse to your current approach that may work is to have an event fire when the user moves off the menu div, but use a timeout to do this.

In addition to this, have an event that fires when you mouse over navigation items in the menu div to clear the close timeout.

This would bacially give the user and opportunity to mouse back in. Try it, see what you think.

$('#selectednavigationdiv').bind("mouseout", close);
$('#selectednavigationitems').bind("mouseover", function() { clearTimeout(hideTimer ); });// stop drop down menu from being closed

var hideTimer = setTimeout(function() {  }, 1);//initialise so not undefined
function close() {
    clearTimeout(hideTimer );
    hideTimer = setTimeout(function() { $('#selectednavigationdiv').slideUp('fast') }, 1000);//close drop down menu                    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文