How to add Event handler in Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add Event handler in Javascript

Hello guys hope you all are doing well so I have a question is there any event listener function in JavaScript so we can track if mouse is like 10 pixels away from our target element and we want to do that event

21st Jun 2021, 11:05 AM
Hussain Ali
Hussain Ali - avatar
3 Answers
0
Make a listener for your mouse movement (onmousemove) and in that function calculate the desired distance. if it was equal to 10px call your final function.
21st Jun 2021, 11:26 AM
Qasem
0
mouse coordinates are stored inside the mouse event objects passed as argument to event listener functions registered with addEventListener... object coordinates are accessible with getBoundingClientRect... however, you must take attention to coordinates relative to screen or relative to page (and if needed add scroll amount to coordinates ;)) https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
21st Jun 2021, 2:12 PM
visph
visph - avatar
0
Thanks brother
22nd Jun 2021, 11:15 AM
Hussain Ali
Hussain Ali - avatar