Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive mousemove events from iframe, too

I have a javascript app, whichs adds a mousemove listener to the document. Problem: When the mouse is moved over an iframe, the function is NOT called.

Is there a way to pass through such events to the root document?

like image 572
Van Coding Avatar asked Mar 10 '11 14:03

Van Coding


People also ask

Can I add event listener to iframe?

Can we add event listener in iframe? You can track this event inside your iframe by using the script below, which adds an event listener to catch the sent message once the ad is displayed to the user.

What is Mousemove event?

The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it.

How are mouse events triggered?

Every mouse move over an element triggers that event. click. Triggers after mousedown and then mouseup over the same element if the left mouse button was used.


2 Answers

Put pointer-events: none; in the styles for the frame.

I was having this problem myself and found this solution works great and is so simple!

like image 152
cloudonshore Avatar answered Sep 21 '22 06:09

cloudonshore


You should look through combining parent document event binding with document.getElementById('iFrameId').contentDocument event, witch allows you to get access to iFrame content elements.

https://stackoverflow.com/a/38442439/2768917

function bindIFrameMousemove(iframe){
    iframe.contentWindow.addEventListener('mousemove', function(event) {
        var clRect = iframe.getBoundingClientRect();
        var evt = new CustomEvent('mousemove', {bubbles: true, cancelable: false});

        evt.clientX = event.clientX + clRect.left;
        evt.clientY = event.clientY + clRect.top;

        iframe.dispatchEvent(evt);
    });
};

bindIFrameMousemove(document.getElementById('iFrameId'));
like image 20
Andrii Verbytskyi Avatar answered Sep 20 '22 06:09

Andrii Verbytskyi