re2.addEventListener("mouseover", function(){
const re21 = document.getElementById("reso");
re21.style.position = "absolute";
re21.style.top = "50%";
re21.style.left = "50%";
console.log("Exiting Listener- mouseOver");
re3.addEventListener("mouseover", function(){
const re22 = document.getElementById("reso");
re22.style.position = "fixed";
re22.style.top ="0px";
re22.style.left="0px";
console.log("Exiting Listener- mouseout");
});
});
I have an html div element with id as "reso".
what i want to do is to move this element from point A to point B whenever the mouse get on to that element. How can we do this?
when we execute the above javascript code. it only moves a single turn. Then we tried with a loop for i=0 to 10 wrapped the above code in it with some console logs. all the logs were printing.
but still the eventlisteners executed only once.
can you help us how to do this?
3
Answers
To move an element from point A to point B when the mouse hovers over it, you can use the mousemove event listener instead of mouseover, which only fires once when the mouse enters the element.
You may need to initialise a counter on an outer scope than your event callback function, then you increment it every time you trigger the event.
you can use the mousemove event instead of the mouseover event. This will allow you to continuously update the position of the element as the mouse moves