[英]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! 谢谢!
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)
})();
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.