React's mouseEvent doesn't have offsetX/offsetY

前端 未结 2 2119
我在风中等你
我在风中等你 2021-02-06 21:10

I try get position of click relative to element, but event doesn\'t have offsetX.

onClick(e) {
  console.log(e.offsetX) // returns undefined
  console.log(e.targ         


        
2条回答
  •  花落未央
    2021-02-06 21:36

    I have found that evt.nativeEvent.offsetX was causing me problems with my component flashing a lot and being sort of weird, I haven't fully debugged it, but I switched to using

    React.createRef or React.useRef on the parent container, and then using event.clientX - ref.current.getBoundingClientRect().left and found this works better for me

提交回复
热议问题