18

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?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Van Coding
  • 24,244
  • 24
  • 88
  • 132

6 Answers6

43

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!

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
cloudonshore
  • 439
  • 3
  • 3
  • 11
    Yes; this is correct , but it'll disable all the other events like scrolling or highlighting . – Ashraf Aug 10 '14 at 06:26
  • @Ashraf working fine pointer-events:none; but its disabling all other events on the iframe loaded document, how did you resolve this problem.thanks – Sujithrao Dec 05 '17 at 12:28
  • 7
    I declared `body.dragging iframe {pointer-events: none;}` in my css and set `document.body.classList.add('dragging');` only while I needed the mousemove to fire. – Kyle Jun 20 '18 at 17:17
6

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'));
Community
  • 1
  • 1
Andrii Verbytskyi
  • 7,155
  • 3
  • 47
  • 38
4

You can do that quite easily if the document in the iframe is on the same document.domain.

If you have the same document.domain, you will have to set a mousemove listener in the iframe as well and pass the values out to the parent page.

If the documents are not on the same document.domain it becomes quite a bit mroe complex, and you will need the iframes page to run javascript itself that sets the mousemove event listener. and then you can do cross frame communication as described here: http://softwareas.com/cross-domain-communication-with-iframes

Otherwise you are out of luck due to the same origin policy that browsers enforce.

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
3

I was having the same problem just now and I came up with this:

$("iframe").contents().find("body").mousemove(function(cursor){
        $("#console").html(cursor.pageX+":"+cursor.pageY);
    });
    $(document).mousemove(function(cursor){
        $("#console").html(cursor.pageX+":"+cursor.pageY);
    });

.contents().find("body") grabs the contents inside the iframe and .mousemove() can be used to add an event listener

Test html...

<div id="console"></div>
Luca
  • 9,259
  • 5
  • 46
  • 59
mark
  • 31
  • 1
2

Though pointer-events: none; in the styles for the frame can solve this problem,but it disabled any other events in iframe,my solution is to toggle the value like:

{{pointer-events : isMoving? 'none' : 'all' }}
1

THIS WORKS FOR ME 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;

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

bindIFrameMousemove(document.getElementById('iFrameId'));
dieb
  • 81
  • 5