简体   繁体   English

悬停在元素上时使功能停止

[英]Make function stop when hovering over an element

I have a function that runs when hovering a parent div and it should stop working when hovering a child div because there is another function for that. 我有一个函数在悬停父div时运行,并且在悬停子div时应停止工作,因为还有另一个函数。

<div class="parent">
//Here the function should work

<div class="child">Text </div> // here it should stop working

//Here the function should work
</div>

I am conditioning it to work on parent div with 我正在调节它以与父div一起工作

if ($('.parent').is(':hover')) {do stuff}

and I am interested in having something like this: 我对这样的事情感兴趣:

if ($('.parent').is(':hover') AND NOT $('.child').is(':hover')) { do stuff }.

How can I achieve this? 我该如何实现?

Thanks! 谢谢!

Use event.stopPropagation() like bellow 像下面这样使用event.stopPropagation()

$('.parent').hover(function(){
    alert('parent');
})

$('.child').hover(function(e){
    e.stopPropagation();
    alert('child');
})

DEMO 演示

As your mouse goes through the parent first, you can't just use stopPropagation : the parent function would activate when the mouse is on the way to the child. 当鼠标首先经过父级时,您不能只使用stopPropagation :当鼠标移至子级时,父级功能将被激活。

A solution in such a case is to use a timer : start the action only if the mouse stops over the parent without entering the child. 在这种情况下,一种解决方案是使用计时器:仅当鼠标悬停在父级上而不进入子级时,才开始操作。

(function(){
    var timer;
    function p(){
        clearTimeout(timer);
        timer = setTimeout(function(){
            alert('parent');
        }, 500);
    }
    $('.parent').hover(p, function(){
        clearTimeout(timer);
    })

    $('.child').hover(function(e){
        e.stopPropagation();
        clearTimeout(timer);
    }, p)
})();

demonstration 示范

  • Use event.stopPropagation() it stops parent elements from knowing about a given event on its child. 使用event.stopPropagation()可以阻止父元素知道其子元素上的给定事件。
  • In your code use 在您的代码中使用

    $('.child').hover(function(event){ event.stopPropagation(); })

  • stopPropogation on a child preventing any parent event handlers from being executed. 子项上的stopPropogation阻止执行任何父级事件处理程序。

暂无
暂无

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

相关问题 悬停在另一个元素上时,使一个元素出现并进行调整 - Make an element appear and make adjustments when hovering over another element 悬停在元素上时使图像跟随光标 - Make image follow cursor when hovering over an element 元素A放置在元素B上。无论如何,当将鼠标悬停在元素A上时,使页面看起来元素B仍在悬停吗? - Element A is positioned over Element B. Anyway, when hovering over Element A, make the page appear that Element B is still being hovered over? jQuery:将鼠标悬停在某个元素上会导致另一个元素的淡入淡出,但是将鼠标悬停在该元素上时它会消失 - jQuery: Hovering over an element brings fades in another element, but when hovering over that element it disappears 当用户将鼠标悬停在 div 上但停止时关闭时,如何使我的下拉菜单保持打开状态? - How can I make my dropdown menu remain open when user is hovering over the div but close when they stop? 模拟将鼠标悬停在元素上 - Emulate hovering over an element 如何在d3中为可视化制作一个函数而不是3个或更多函数,然后将鼠标悬停在节点上时添加突出显示 - How to make one function instead of 3 or more for a visualization in d3 to then add highlight when hovering over nodes 当鼠标悬停在鼠标上方时如何停止mouseOver文本闪烁 - How to stop mouseOver text from flickering when the mouse is hovering over it 将鼠标悬停在图像上方时,请停止旋转图像以免暂停 - Stop rotating images from pausing when hovering over them 将鼠标悬停在元素B上时更改元素A的背景-mouseleave不起作用 - changing the background of element A when hovering over element B - mouseleave not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM