-
-
Save Rabbitzzc/9dd1f60d532880bb45e5d565e6709826 to your computer and use it in GitHub Desktop.
Bubbling iframe events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bubbleIframeEvents = (iframe, document) => { | |
const iframeWindow = iframe.contentWindow | |
iframeWindow.addEventListener( | |
'mousemove', | |
(event) => { | |
const boundingClientRect = iframe.getBoundingClientRect() | |
const fakeEvent = new CustomEvent( | |
'mousemove', | |
{ | |
bubbles : true, | |
cancelable : false, | |
} | |
) | |
fakeEvent.clientX = event.clientX + boundingClientRect.left | |
fakeEvent.clientY = event.clientY + boundingClientRect.top | |
iframe.dispatchEvent(fakeEvent) | |
} | |
) | |
iframeWindow.addEventListener( | |
'mousedown', | |
(event) => { | |
const fakeEvent = new CustomEvent( | |
'mousedown', | |
{ | |
bubbles : true, | |
cancelable : false, | |
} | |
) | |
iframe.dispatchEvent(fakeEvent) | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
还可以增加其他的事件监听