Detect mousemove when over an iframe?

后端 未结 11 573
Happy的楠姐
Happy的楠姐 2020-11-29 05:47

I have an iframe that takes up the entire window (100% wide, 100% high), and I need the main window to be able to detect when the mouse has been moved.

Already tried

11条回答
  •  既然无缘
    2020-11-29 06:37

    Here is my solution with jQuery. You can detect multiple events as I do below. Putting the .on() event handler inside the .on('load') event handler is necessary, because it would stop detecting the iframe content events when the iframe navigates to a new page otherwise. Additionally, I believe this only works if the iframe content is on the same domain as the parent page due to security, and there is no way around that.

    $(document).ready(function() {
        $('#iframe_id').on('load', function() {
            $('#iframe_id').contents().on('click mousemove scroll', handlerFunction);
        });
    });
    
    handlerFunction() {
        //do stuff here triggered by events in the iframe
    }
    

提交回复
热议问题