简体   繁体   English

禁用带有单击功能的jQuery悬停功能

[英]Disabling a jquery hover function with a click function

We have a gallery, with a caption that fades in when you hover your cursor over the image div. 我们有一个图库,带有标题,当您将光标悬停在图像div上时,标题会淡入。 Our next step is to add a button that will show all the captions, so folks can scroll and read without running their cursor all over the screen. 我们的下一步是添加一个显示所有字幕的按钮,这样人们就可以滚动和阅读,而无需在整个屏幕上运行光标。

Creating a showall function is easy, but when the cursor passes over any photo, the hover function fires and the caption on that image disappears. 创建showall函数很容易,但是当光标经过任何照片时,将触发悬停功能,并且该图像上的标题会消失。

Is there an easy way to have the showall function disable the hover function when the showall is active? 当showall处于活动状态时,是否有一种简单的方法可以让showall函数禁用悬停功能?

Our basic jquery code is below, .tease is the caption: 我们的基本jquery代码如下,标题为.tease:

$('.tease').hide();
$('.img33, .img40, .img50, .img60').hover(
    function(){ $(this).children('.tease').fadeIn('900'); },
    function(){ $(this).children('.tease').fadeOut('2500'); }
);

The basic html for each image: 每个图像的基本html:

<div class="img33">
<img src="../dir/someimage.jpg">
<div class="tease">Tease copy</div>
</div>

You could just define a global variable that you check against in the hover function. 您只需在悬停函数中定义要检查的全局变量即可。

For example 例如

var hoverEnabled = true;

$('.tease').hide();
$('.img33, .img40, .img50, .img60').hover(
    function(){ if(hoverEnabled) $(this).children('.tease').fadeIn('900'); },
    function(){ if(hoverEnabled) $(this).children('.tease').fadeOut('2500'); }
);

$('#showAllButton').click(function() {
    $('.tease').show();
    hoverEnabled = false;
}

Alternatively you could bind your hover event using .bind() and event name (for example hover.showTease) and .unbind() it in your showall function. 另外,您也可以在showall函数中使用.bind()和事件名称(例如hover.showTease)将其悬停事件与.unbind()绑定。

You can unbind "hover" this way: 您可以通过以下方式解除“悬停”的绑定:

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

Just do this when you want to disable the hover. 如果要禁用悬停,只需执行此操作。 Alternatively, to have greater control and not keep adding and removing events, you may want to introduce a class that is added and removed, and only execute hover actions if certain class is set to the element. 另外,为了具有更好的控制权而又不保持添加和删除事件,您可能想要引入一个添加和删除的类,并且仅在将某些类设置为元素时才执行悬停操作。 Like this: 像这样:

$(this).hover(
    function() { 
        if ($(".yourElement").hasClass("allowHover")) { 
            ... 
        }  
    },
    function() { 
        if ($(".yourElement").hasClass("allowHover")) { 
            ... 
        }  
    }
 );

Then just add or remove a class and the hover will be enabled or disabled. 然后只需添加或删除一个类,悬停就会启用或禁用。

var allShown = false;

function showAll(){
    //some imaginary code

    //we're showing all
    allShown = true;
}

This should be ni the scope of the DOM ready. 这应该是DOM准备就绪的范围。

$(function(){
   $('.img33, .img40, .img50, .img60').hover(function(){ 
        if(allShown === false){
            $(this).children('.tease').fadeIn('900');
        }
    },function(){  
        if(allShown === false){
            $(this).children('.tease').fadeOut('2500');
        }
    });

    $('ele').on('something', function(){
        //some code..
        allShown = false;
    });

    //some event to fire the showAll function
    $('ele').on('something', function(){
        showAll();
        allShown = true;
    });



});

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

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