The document will not fire the mousemove event if mousedown occurred in the iframe

I have the same iframe origin. Mouse events in an iframe are triggered in a document as follows:

// this won't work $('iframe').contents().find('body').on('mousedown mouseup mousemove', function(e) { $(document).trigger(e); }); // this does work $('iframe').contents().on('mousedown mouseup mousemove', function(e) { $(document).trigger(e); }); 

My problem is that mousedown occurs in an iframe, and the mouse leaves the iframe, the document does not fire its own mousemove events until mouseup appears.

I tried to run mouseup in both the iframe and the document when the mouse leaves the iframe, but the mousemove events of the document will not resume until a physical mouse occurs.

+4
source share
3 answers

using object notation, you can add multiple events to .on (). then I added .contents () so that all your events work in an iframe. here is a working violin

 $('.myiframe').contents().on({ mousedown: function () { console.log('mouse down triggered'); }, mouseup: function () { console.log('mouse up triggered'); }, mousemove: function() { console.log('mouse move triggered'); } }); 
0
source

This is what worked for me on a page with multiple iFrames:

 $(function() { $('iframe').each(function(index) { $(this).load(function (a, b, c) { $(this).contents().on("mousedown", function (e) { $(document).trigger(e); }) .on("mouseup", function (e) { $(document).trigger(e); }); }); }); }); 

It will only work with one iframe. The important part is to wait until the frame is loaded before binding events, otherwise it may not work. In my case, mouse events were detected correctly in one iframe, but not in another.

+1
source

When you call the above code from the page, it takes some time to load the frame body. Therefore, it cannot connect the mousemove event listener framework. If you call it using the setimeout function, it will be able to get the contents of the frame, and movemove will depend on the body of the frame.

:)

-1
source

Source: https://habr.com/ru/post/1482745/


All Articles