reactjs - What is the proper way to get the mouse position in a React Component? -
i have component renders canvas element. have onmousemove
handler, when move mouse towards top of element event.clienty
prints 86. why not 0? there synthetic event property give me point relative element's coordinate system , not windows?
you can use targets offsettop
relative position subtracting clienty
like
event.clienty - event.target.offsettop
you can see example
class app extends react.component{ render(){ return( <div> <div>top component</div> <div classname="main" onmousemove={(e) => console.log(e.clienty - e.target.offsettop)}>main component</div> </div> ) } } reactdom.render(<app/>, document.getelementbyid("root"));
div{ min-height: 50px; } div.main{ background-color: blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div>
Comments
Post a Comment