简体   繁体   English

暂时停用.hover

[英]Temporarily disabling .hover

I'm trying to figure out how to temporarily disable .hover in jquery due to an event on the page. 我试图弄清楚如何由于页面上的事件而在jquery中临时禁用.hover。

My current jsfiddle looks like this.. http://jsfiddle.net/4vhajam3/3/ (note that I've lopped off a lot of code for sanity's sake) 我当前的jsfiddle看起来像这样。.http: //jsfiddle.net/4vhajam3/3/ (请注意,为了理智起见,我已经舍弃了很多代码)

The current page setup is that if you mouse over any with the class "toqc", the image to QC appears in a div below. 当前页面设置是,如果将鼠标悬停在任何类“ toqc”上,则要质量控制的图像显示在下面的div中。 My need is that if a user clicks on one of the table cells, the mouseover is temporarily disabled (say for 10 seconds) so that they can move around on the page a bit without changing the image in the div. 我需要的是,如果用户单击表格单元格之一,则鼠标悬停将被暂时禁用(例如10秒钟),以便他们可以在页面上四处移动而不更改div中的图像。

I looked at a few other questions on this site (namely jQuery: temporarily disable hover... unintended side effect ) and while I understand the code, I can't seem to modify it to work for me. 我在这个网站上查看了其他一些问题(即jQuery:暂时禁用悬停...意想不到的副作用 ),虽然我理解了代码,但似乎无法对其进行修改以使其适合我。 Specifically I was trying 我特别在尝试

    var hoverEnabled = true;

$('.toqc').click(
    function(){
        hoverEnabled = false;
    });

if(hoverEnabled){
    $('#ussfcanl').hover(function() {
    $('#imageholder').html('<img src="http://www.hpc.ncep.noaa.gov/sfc/satsfc.gif" />');                              
});
}

But yet, even after clicking on something with the class .toqc, the hover still goes on as soon as I move to another .toqc class. 但是,即使在单击类.toqc的内容后,只要我移到另一个.toqc类,悬停仍然会继续。

Any help would be greatly appreciated..Kinda lost on where to go from here with my code. 任何帮助将不胜感激..Kinda迷失了我的代码从何而来。 Thanks! 谢谢!

Put the condition inside, that way it will fire but do nothing. 将条件放在里面,这样它将触发但什么也不做。

$('#ussfcanl').hover(function() {
    if(hoverEnabled){
        $('#imageholder').html('<img src="http://www.hpc.ncep.noaa.gov/sfc/satsfc.gif" />');                              
    }
});

Initially when loading the page the hover function is created. 最初,在加载页面时会创建悬停功能。 Therefore the hover still works after you change the hoverEnabled var. 因此,在更改hoverEnabled变量后,悬停仍然有效。

You should check the state of the hoverEnabled within the function like so: 您应该像这样检查函数中hoverEnabled的状态:

//written this way, the function seems to make more sense
$('#ussfcanl').on('mouseenter', function() {
    //checking the type just in case
    if(hoverEnabled === true) {
        $('#imageholder').html('<img src="http://www.hpc.ncep.noaa.gov/sfc/satsfc.gif" />');                              
    }
});

This allows the hover event to become re-enabled after 10 seconds: 这允许悬停事件在10秒后重新启用:

$('.toqc').click(function(){
  hoverEnabled = false;
  clearTimeout(timer);
  timer= setTimeout(function(){
    hoverEnabled=  true;
  },10000);
});

Replace your hover events with the following, which allows you to change images on click even when hoverEnabled is false: 将您的hover事件替换为以下内容,即使hoverEnabled为false,也可以在单击时更改图像:

$('#ussfcanl').on('mouseover click', function(event) {
  if(event.type!=='mouseover' || hoverEnabled) {
    $('#imageholder').html('<img src="http://www.hpc.ncep.noaa.gov/sfc/satsfc.gif" />');                              
  }
});

$('#mexsfcanl').on('mouseover click',function(event) {
  if(event.type!=='mouseover' || hoverEnabled) {
    $('#imageholder').html('<img src="http://www.hpc.ncep.noaa.gov/sfc/namaksfcwbg.gif" />');
  }
});

Working Fiddle 工作小提琴

There are two aspects to .hover() that you seem to be missing. 您似乎缺少.hover()两个方面。

The first is that events are bound on page load. 首先是事件绑定在页面加载上。 That's why, as other answers have mentioned, you need to put the condition inside the event fired callback (the function() {} part), like so: 这就是为什么,正如其他答案所提到的,您需要将条件放入触发事件的回调( function() {}部分)中,如下所示:

$('#ussfcanl').hover(function() {
    if(hoverEnabled){
        // do stuff on hover
    }
});

The second part is that the jQuery .hover() method is really a shorthand for the mouseenter and mouseleave events. 所述第二部分是jQuery的.hover()方法是真正为一个速记mouseentermouseleave事件。 Calling .hover() with one function will cause that function to fire for both events, which doesn't seem to be what you want. 使用一个函数调用.hover()会导致该函数针对两个事件触发,这似乎并不是您想要的。

So you probably want to use two callback functions with this method, the first to tell the browser what to do when the mouseenter event is fired, and the second for the mouseleave event. 因此,您可能要使用此方法使用两个回调函数,第一个告诉浏览器在激发mouseenter事件时该怎么做,第二个告诉mouseleave事件。 See the jQuery docs on .hover() 请参阅.hover()上的jQuery文档

$('#ussfcanl').hover(
    function() {
        if(hoverEnabled){
            // do stuff on mouseenter
        }
    },
    function() {
        if(hoverEnabled){
            // do stuff on mouseleave
        }
    }
);

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

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