简体   繁体   English

如何unbind()。hover()而不是.click()?

[英]How to unbind() .hover() but not .click()?

I'm creating a site using Bootstrap 3, and also using a script that makes the dropdown-menu appear on hover using the .hover() function. 我正在使用Bootstrap 3创建一个站点,并且还使用一个脚本,使用.hover()函数使下拉菜单显示在悬停状态。 I'm trying to prevent this on small devices by using enquire.js. 我试图通过使用enquire.js在小型设备上防止这种情况。 I'm trying to unbind the .hover() event on the element using this code: 我正在尝试使用此代码取消绑定元素上的.hover()事件:

$('.dropdown').unbind('mouseenter mouseleave');

This unbinds the .hover of that script but apparently it also removes the .click() event(or whatever bootstrap uses), and now when I hover or click on the element, nothing happens. 这解除了该脚本的.hover绑定,但显然它也删除了.click()事件(或任何引导程序使用),现在当我悬停或点击该元素时,没有任何反应。

So I just want to how I can remove the .hover() on that element, that is originating from that script, but not change anything else. 所以我只想知道如何删除该元素上的.hover(),该元素来自该脚本,但不会更改任何其他内容。

Would really appreciate any help. 非常感谢任何帮助。

Thanks! 谢谢!

Edit: Here is how I'm calling the handlers for the hover functions: 编辑:这是我如何调用悬停函数的处理程序:

$('.dropdown').hover(handlerIn, handlerOut);

function handlerIn(){
   // mouseenter code
}
function hideMenu() {
   // mouseleave code
}

I'm trying to unbind them with this code. 我正试图用这段代码解开它们。

$('.dropdown').unbind('mouseenter', showMenu);
$('.dropdown').unbind('mouseleave', hideMenu);

But its not working. 但它不起作用。

Please help! 请帮忙!

**Edit2: ** Based on the answer of Tieson T.: **编辑2:**根据Tieson T的回答:

function dropdownOnHover(){
  if (window.matchMedia("(min-width: 800px)").matches) {
    /* the view port is at least 800 pixels wide */
    $('.dropdown').hover(handlerIn, handlerOut);

    function handlerIn(){
       // mouseenter code
    }
    function hideMenu() {
       // mouseleave code
    }
  }
}

$(window).load(function() {
  dropdownOnHover();
});

$(window).resize(function() {
  dropdownOnHover();
});

The code that Tieson T. provided worked the best; Tieson T.提供的代码效果最好; however, when I resize the window, until I reach the breakpoint from any direction, the effect doesn't change. 但是,当我调整窗口大小时,直到我从任何方向到达断点,效果不会改变。 That is, if the window is loaded above 800px, the hover effect will be there, but if I make the window smaller it still remains. 也就是说,如果窗口加载到800px以上,则悬停效果将存在,但如果我使窗口变小,它仍然存在。 I tried to invoke the functions with window.load and window.resize but it is still the same. 我试图用window.load和window.resize调用这些函数,但它仍然是相同的。

Edit 3: I'm actually trying to create Bootstrap dropdown on hover instead of click. 编辑3:我实际上是在悬停而不是点击时创建Bootstrap下拉列表。 Here is the updated jsFiddle: http://jsfiddle.net/CR2Lw/2/ 这是更新的jsFiddle: http//jsfiddle.net/CR2Lw/2/

Please note: In the jsFiddle example, I could use css :hover property and set the dropdow-menu to display:block. 请注意:在jsFiddle示例中,我可以使用css:hover属性并将dropdow-menu设置为display:block。 But because the way I need to style the dropdown, there needs to be some space between the link and the dropdown (it is a must), and so I have to find a javascript solution. 但是因为我需要为下拉列表设置样式,所以链接和下拉列表之间需要有一些空间(这是必须的),所以我必须找到一个javascript解决方案。 or a very tricky css solution, in which the there is abot 50px space between the link and the dropdown, when when the user has hovered over the link and the dropdown has appeared, the dropdown shouldn't disappear when the user tries to reach it. 或者是一个非常棘手的css解决方案,其中链接和下拉列表之间有大约50px的空间,当用户在链接上盘旋并且下拉列表出现时,当用户试图访问它时,下拉列表不应该消失。 Hope it makes sense and thanks. 希望它有意义并且谢谢。

Edit 4 - First possible solution: http://jsfiddle.net/g9JJk/6/ 编辑4 - 第一个可能的解决方案: http //jsfiddle.net/g9JJk/6/

Might be easier to selectively apply the hover, rather than try to remove it later. 可能更容易选择性地应用悬停,而不是稍后尝试删除它。 You can use window.matchMedia and only apply your script if the browser has a screen size that implies a desktop browser (or a largish tablet): 您可以使用window.matchMedia并仅在浏览器的屏幕大小暗示桌面浏览器(或较大的平板电脑)时应用您的脚本:

if (window.matchMedia("(min-width: 800px)").matches) {
    /* the view port is at least 800 pixels wide */
    $('.dropdown').on({
        mouseenter: function () {
            //stuff to do on mouse enter
        },
        mouseleave: function () {
            //stuff to do on mouse leave
        }
    });
}
else{
    $('.dropdown').off('mouseenter, mouseleave');
}

Since it's not 100% supported, you'd want to add a polyfill for those browsers without native support: https://github.com/paulirish/matchMedia.js/ 由于它不是100%支持,您需要为没有本机支持的浏览器添加polyfill: https//github.com/paulirish/matchMedia.js/

If you're using Moderizr, that polyfill is included in that library already, so you're good-to-go. 如果你正在使用Moderizr,那个polyfill已经包含在那个库中了,所以你可以随意使用。

I still don't understand how you intend to "dismiss" the dropdown-menu once it is displayed upon mousing over the dropdown element partly because there's not enough code in your question, but that's sort of irrelevant to this answer. 我仍然不明白你是否打算在鼠标悬停在下拉元素上时显示“关闭”下拉菜单,部分原因是因为你的问题中没有足够的代码,但这与这个答案无关。

I think a much easier way to approach the mousenter event handling portion is not by using off()/on() to unbind/bind events at a specific breakpoints, but rather to do just do a simple check when the event is triggered. 我认为更简单的方法来处理mousenter事件处理部分不是通过使用off()/ on()来取消绑定/绑定特定断点处的事件,而是在触发事件时执行简单的检查。 In other words, something like this: 换句话说,这样的事情:

$('.dropdown').on('mouseenter', function() {
    if($('.navbar-toggle').css('display') == 'none') {
       $(this).children('.dropdown-menu').show();
    };
});

$('.dropdown-menu').on('click', function() {
    $(this).hide();
});

Here's a working fiddle: http://jsfiddle.net/jme11/g9JJk/ 这是一个工作小提琴: http//jsfiddle.net/jme11/g9JJk/

Basically, in the mouseenter event I'm checking if the menu toggle is displayed, but you can check window.width() at that point instead if you prefer. 基本上,在mouseenter事件中,我正在检查是否显示了菜单切换,但是如果您愿意,可以在该点检查window.width()。 In my mind, the toggle element's display value is easier to follow and it also ensures that if you change your media query breakpoints for the "collapsed" menu, the code will remain in sync without having to update the hardcoded values (eg 768px). 在我看来,切换元素的显示值更容易理解,它还确保如果您更改“折叠”菜单的媒体查询断点,代码将保持同步,而无需更新硬编码值(例如768px)。

The on click to dismiss the menu doesn't need a check, as it has no detrimental effects that I can see when triggered on the "collapsed" menu dropdown. 单击以关闭菜单不需要检查,因为它没有在“折叠”菜单下拉列表中触发时可以看到的有害影响。

I still don't like this from a UX perspective. 从UX的角度来看,我仍然不喜欢这样。 I would much rather have to click to open a menu than click to close a menu that's being opened on a hover event, but maybe you have some magic plan for some other way of triggering the hide method. 我宁愿点击打开一个菜单而不是点击关闭一个在悬停事件中打开的菜单,但也许你有一些神奇的计划用于触发hide方法的其他方式。 Maybe you are planning to register a mousemove event that checks if the mouse is anywhere within the bounds of the .dropdown + 50px + .dropdown-menu or something like that... I would really like to know how you intend to do this (curiosity is sort of killing me). 也许你打算注册一个mousemove事件,检查鼠标是否在.dropdown + 50px + .dropdown-menu的范围内的任何地方或类似的东西......我真的想知道你打算怎么做(好奇心有点让我害怕)。 Maybe you can update your code to show the final result. 也许您可以更新代码以显示最终结果。

EDIT: Thanks for posting your solution! 编辑:感谢您发布您的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM