jQuery - 取消绑定或重新绑定hoverIntent()?

发布于 2024-12-15 14:04:53 字数 266 浏览 2 评论 0原文

我有一个菜单栏,在上一行显示一组类别。

其中一个类别具有一组子类别。

我有一个悬停意图设置,以便它会在子菜单中向下滑动,并在鼠标离开时向上滑动。

但是,如果我正在查看此类别中的页面,我希望子菜单可见,并突出显示活动类别。我还想确保当通过鼠标与子菜单交互时,一旦鼠标离开,它不会再次向上滑动。

我尝试在此页面中的元素上重新声明hoverIntent函数,但它不起作用,它仍然使用以前的绑定。有没有办法解除之前的hoverIntent的绑定并确保它使用新的?

I have a menu bar that displays a set of categories in an upper row.

One of the categories has a set of sub-categories.

I have a hoverIntent setup so that it will slideDown the submenu, and slideUp when the mouse leaves.

However, if I am viewing a page in this category, I would like the submenu to be visible with the active category highlighted. I would also like to make sure that when the submenu is interacted with via the mouse, it does not slideUp again once the mouse leaves.

I have tried redeclaring the hoverIntent function on the element in this page but it does not work, it is still using the previous binding. Is there any way to unbind the previous hoverIntent and make sure it uses the new one?

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

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

发布评论

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

评论(4

回忆那么伤 2024-12-22 14:04:53

要绑定和取消绑定 hoverIntent 你应该这样做:

// bind the hoverIntent
$("#demo1 li").hoverIntent(makeTall, makeShort)
// unbind the hoverIntent
$("#demo1 li").unbind("mouseenter").unbind("mouseleave");
$("#demo1 li").removeProp('hoverIntent_t');
$("#demo1 li").removeProp('hoverIntent_s');
// rebind the hoverIntent
$("#demo1 li").hoverIntent(makeTall, makeShort)

to bind and unbind the hoverIntent you should do:

// bind the hoverIntent
$("#demo1 li").hoverIntent(makeTall, makeShort)
// unbind the hoverIntent
$("#demo1 li").unbind("mouseenter").unbind("mouseleave");
$("#demo1 li").removeProp('hoverIntent_t');
$("#demo1 li").removeProp('hoverIntent_s');
// rebind the hoverIntent
$("#demo1 li").hoverIntent(makeTall, makeShort)
百善笑为先 2024-12-22 14:04:53

我认为这是一个更完整的答案。它执行以下操作:

  • 清除所有活动计时器。
  • 所有事件都被清除
  • 所有对象属性都被清除
  • 使用常见的 jQuery 语法,看起来像hoverIntent

代码的本机部分:

(function($) {
   if (typeof $.fn.hoverIntent === 'undefined')
     return;

   var rawIntent = $.fn.hoverIntent;

   $.fn.hoverIntent = function(handlerIn,handlerOut,selector) 
    {
      // If called with empty parameter list, disable hoverintent.
      if (typeof handlerIn === 'undefined')
      {
        // Destroy the time if it is present.
        if (typeof this.hoverIntent_t !== 'undefined') 
        { 
          this.hoverIntent_t = clearTimeout(this.hoverIntent_t); 
        }
        // Cleanup all hoverIntent properties on the object.
        delete this.hoverIntent_t;
        delete this.hoverIntent_s;

        // Unbind all of the hoverIntent event handlers.
        this.off('mousemove.hoverIntent,mouseenter.hoverIntent,mouseleave.hoverIntent');

        return this;
      }  

      return rawIntent.apply(this, arguments);
    };  
})(jQuery);

I think this is a more complete answer. It does the following:

  • Any active timer is cleaned up.
  • All events are cleared
  • All object properties are cleared
  • Uses common jQuery syntax and looks like native part of hoverIntent

Code:

(function($) {
   if (typeof $.fn.hoverIntent === 'undefined')
     return;

   var rawIntent = $.fn.hoverIntent;

   $.fn.hoverIntent = function(handlerIn,handlerOut,selector) 
    {
      // If called with empty parameter list, disable hoverintent.
      if (typeof handlerIn === 'undefined')
      {
        // Destroy the time if it is present.
        if (typeof this.hoverIntent_t !== 'undefined') 
        { 
          this.hoverIntent_t = clearTimeout(this.hoverIntent_t); 
        }
        // Cleanup all hoverIntent properties on the object.
        delete this.hoverIntent_t;
        delete this.hoverIntent_s;

        // Unbind all of the hoverIntent event handlers.
        this.off('mousemove.hoverIntent,mouseenter.hoverIntent,mouseleave.hoverIntent');

        return this;
      }  

      return rawIntent.apply(this, arguments);
    };  
})(jQuery);
终陌 2024-12-22 14:04:53

来自 jQuery 文档:“在最简单的情况下,没有参数,.unbind() 会删除附加到元素的所有处理程序”

From jQuery docs: "In the simplest case, with no arguments, .unbind() removes all handlers attached to the elements"

信愁 2024-12-22 14:04:53

我用过:

var elements = $("#main_nav li, #breadcrumb_ul li");
elements.unbind('mouseover mouseout');
delete $(elements).hoverIntent_t;
delete $(elements).hoverIntent_s;

I used:

var elements = $("#main_nav li, #breadcrumb_ul li");
elements.unbind('mouseover mouseout');
delete $(elements).hoverIntent_t;
delete $(elements).hoverIntent_s;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文