简体   繁体   English

当 hover 超过孩子时防止触发 onMouseOut 事件

[英]Prevent triggering onMouseOut event when hover over children

I have a div element and two children inside it.我有一个div元素和里面的两个孩子。 I want to show .portfolio-overlay when user hover over .portfolio-item and hide it when they leave that element.我想在用户 hover 超过.portfolio-item时显示.portfolio-overlay并在他们离开该元素时隐藏它。

const handleHover = () => {
        gsap.fromTo(overlay.current, {opacity: 0}, {opacity: 1, duration: .5});
    };

const handleLeave = () => {
     gsap.fromTo(overlay.current, {opacity: 1}, {opacity: 0, duration: .5});
};

<div className="portfolio-item" onMouseOver={handleHover} onMouseOut={handleLeave}>
     <div ref={overlay} className="portfolio-overlay">
           <h3>My header1</h3>
           <h4>My header2</h4>
     </div>
</div>

As you can see, I did that with gsap .如您所见,我使用gsap做到了这一点。 But the problem is when I hover over h3 or h4 , handleLeave() and handleHover() functions are triggered and I see both animations again.但问题是当我 hover 通过h3h4时, handleLeave handleLeave()handleHover()函数被触发,我再次看到两个动画。 How could I prevent that?我怎么能阻止呢?

You can set the attribute "tabindex='-1'".您可以设置属性“tabindex='-1'”。

const handleHover = () => {
    gsap.fromTo(overlay.current, {opacity: 0}, {opacity: 1, duration: .5});
};

const handleLeave = () => {
     gsap.fromTo(overlay.current, {opacity: 1}, {opacity: 0, duration: .5});
};

<div tabindex="-1" className="portfolio-item" onMouseOver={handleHover} onMouseOut={handleLeave}>
     <div ref={overlay} className="portfolio-overlay">
           <h3>My header1</h3>
           <h4>My header2</h4>
     </div>
</div>

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

相关问题 当鼠标悬停在特定子元素上时,禁用父项上的悬停事件 - Disable hover event on parent when mouse is over specific children elements 当将鼠标悬停在覆盖第一个的另一个项目上时,停止触发悬停的“ out”事件 - Stop a hover's 'out' event from triggering when hovering over another item overlaid over the first 当我仍将鼠标悬停在标签上时,会发生onmouseout事件 - onmouseout event happens when I am still hovering over the label 鼠标悬停在元素上时,使用什么事件代替onmouseout? - What event to use instead of onmouseout to run when mouse is not over the element? 当我将光标移动到元素上而不离开元素时,将调用 onmouseout JavaScript 事件 - onmouseout JavaScript event being called when I move the cursor over an element without going out of the element 当我在javascript中删除鼠标当前位于上方的元素时,会发生onMouseOut事件吗? - Does onMouseOut event occurs when I remove in javascript the element the mouse is currently over? 为在javascript中未定义的儿童触发点击事件时 - When triggering click event for children getting Undefined in javascript 防止在 raphael 中触发 div 的悬停触发 - Prevent hover triggering for div in raphael 防止事件触发 - Prevent event from triggering 防止多个事件触发 - Prevent multiple event triggering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM