简体   繁体   English

可以像 React.js 中的 onMouseEnter 一样使用我 stopPropagation 的 mouseHover 吗?

[英]Can use I stopPropagation of mouseHover like onMouseEnter in React.js?

I use onMouseEnter on a parent div.我在父 div 上使用 onMouseEnter。 when I mouse hover on a child element the parent mouseHover is fired fired and I want to stop it.当我将鼠标悬停在子元素上时,父 mouseHover 被触发,我想停止它。 I used e.stopPropagation() but also the parent mouseHover fired.我使用了 e.stopPropagation() 但也触发了父 mouseHover。 When I mouseOver on the child element, The parent mousehover is fired.当我鼠标悬停在子元素上时,父鼠标悬停被触发。 I prevent this parent's mouseHover Event.我阻止了这个父母的 mouseHover 事件。 onMouseEnter={(e)=> e.stopPropagation()} I am using this on the child element to stop the parent mouseHover. onMouseEnter={(e)=> e.stopPropagation()}我在子元素上使用它来停止父鼠标悬停。 But still parent mouseHover is fired.但仍会触发父 mouseHover。

How can I stop Event Bubbleing for onMouseEnter ???如何停止 onMouseEnter 的事件冒泡???

Or, is it possible to stop Event Bubbleing for mouseHove like onMouseEnter?或者,是否可以像 onMouseEnter 一样停止 mouseHove 的事件冒泡?

有功能和状态

在 JSX 上使用 onMouseEnter

 import React, { useEffect, useState } from 'react'; import './Slider.css' import { Swiper, SwiperSlide} from 'swiper/react'; import SwiperCore, {Navigation, Pagination} from 'swiper'; const Slider = ({works}) => { const [isShown, setIsShown] = useState(false) const [cssProperty, setCssProperty] = useState({}); const mouseHover = (e) => { const {width, left, top, bottom} = e.target.parentNode.getBoundingClientRect() setCssProperty({width, left, top, bottom, opacity:1}) console.log(left) } const mouseLeave = (e) => { setIsShown(false) setCssProperty({opacity:0, mouseLeaveClass:"mouseLeaveClass"}) } return ( <div> <Swiper> { works.map((work,index) => ( <SwiperSlide key={`slide-${index}`} tag="li" style={{listStyle:"none"}} className="" > <div onMouseEnter={(e)=> mouseHover(e)} onMouseLeave={() => mouseLeave()} className="job-item" > <div className="job-info" style={{marginBottom:"5px", margin: "0 10px"}}> <div className="job-dates" > <span className="single-job-date"> <span className="job-date-items"> {work?.month && <span className="month" style={{color: "#fff", fontSize:"1rem", opacity:".7"}} onMouseEnter={e=>e.stopPropagation()} > {work?.month} </span>} {work?.startDate && <span className="date" style={{color: "#fff", fontSize:"32px"}} > {work?.startDate} </span>} <br/> </span> </span> <span className="end-single-job-date" > <span className="job-date-items" style={{marginRight:"15px"}}> {work?.endMonth && <span className="month" style={{color: "#fff", fontSize:"1rem", opacity: ".7"}} onMouseEnter={(e)=> e.stopPropagation()}> {work?.endMonth} </span>} {work?.endDate && <span className="date" style={{color: "#fff", fontSize:"32px", }} onMouseEnter={(e)=> e.stopPropagation()}> {work?.endDate} </span>} <br/> </span> </span> </div> <div class="job__title" style={{height: "110px", fontSize:"23px",color: "white !important", fontWeight:"500", transition:"all .5s", position:"relative"}}> {work.company} <br/> <span style={{fontSize:"16px",}}> {work.position} </span> </div> </div> </div> </SwiperSlide> ) ) } </Swiper> </div> ); }; export default Slider;

In the meantime just to confirm, as you are not adding any behaviour on the child element on mouseEnter, just add the below styles to all elements where pointer events are not needed.同时确认一下,由于您没有在 mouseEnter 上的子元素上添加任何行为,只需将以下样式添加到不需要指针事件的所有元素。

pointer: none;

Ofcourse other solution is to check the target of the event and if it's not the one intended, just skip the rest part of the logic.当然,其他解决方案是检查事件的目标,如果它不是预期的目标,则跳过逻辑的其余部分。

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

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