Receive mousemove events from iframe, too

前端 未结 6 1157
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 08:17

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 wa

相关标签:
6条回答
  • 2020-12-03 08:49

    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'));
    
    0 讨论(0)
  • 2020-12-03 08:49

    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'));
    
    0 讨论(0)
  • 2020-12-03 08:52

    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>
    
    0 讨论(0)
  • 2020-12-03 08:53

    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!

    0 讨论(0)
  • 2020-12-03 09:03

    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' }}
    
    0 讨论(0)
  • 2020-12-03 09:11

    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.

    0 讨论(0)
提交回复
热议问题