我有一个占据整个窗口(100%宽,100%高)的 iframe,需要主窗口能够检测到鼠标移动。
已经尝试在iframe上使用 onMouseMove 属性,但显然没有起作用。还尝试将 iframe 包装在 div 中,如下所示:
<div onmousemove="alert('justfortesting');"><iframe src="foo.bar"></iframe></div>
...但它没有起作用。有什么建议吗?
我有一个占据整个窗口(100%宽,100%高)的 iframe,需要主窗口能够检测到鼠标移动。
已经尝试在iframe上使用 onMouseMove 属性,但显然没有起作用。还尝试将 iframe 包装在 div 中,如下所示:
<div onmousemove="alert('justfortesting');"><iframe src="foo.bar"></iframe></div>
...但它没有起作用。有什么建议吗?
如果你的目标浏览器不是Opera 9或更低版本以及IE 9或更低版本,你可以使用CSS属性pointer-events: none
。
我发现忽略iframe是最好的方法。在onMouseDown
事件中,我为iframe添加了一个带有这个属性的类,并在onMouseUp
事件中删除该类。
对我来说效果非常好。
框架可以捕获鼠标事件,但是如果符合跨域政策,您可以将事件传输到父级作用域。以下是方法:
// This example assumes execution from the parent of the the iframe
function bubbleIframeMouseMove(iframe){
// Save any previous onmousemove handler
var existingOnMouseMove = iframe.contentWindow.onmousemove;
// Attach a new onmousemove listener
iframe.contentWindow.onmousemove = function(e){
// Fire any existing onmousemove listener
if(existingOnMouseMove) existingOnMouseMove(e);
// Create a new event for the this window
var evt = document.createEvent("MouseEvents");
// We'll need this to offset the mouse move appropriately
var boundingClientRect = iframe.getBoundingClientRect();
// Initialize the event, copying exiting event values
// for the most part
evt.initMouseEvent(
"mousemove",
true, // bubbles
false, // not cancelable
window,
e.detail,
e.screenX,
e.screenY,
e.clientX + boundingClientRect.left,
e.clientY + boundingClientRect.top,
e.ctrlKey,
e.altKey,
e.shiftKey,
e.metaKey,
e.button,
null // no related element
);
// Dispatch the mousemove event on the iframe element
iframe.dispatchEvent(evt);
};
}
// Get the iframe element we want to track mouse movements on
var myIframe = document.getElementById("myIframe");
// Run it through the function to setup bubbling
bubbleIframeMouseMove(myIframe);
你现在可以监听iframe元素或其任何父元素的mousemove事件 - 事件会像预期的那样冒泡。createEvent
,initMouseEvent
和dispatchEvent
。我用过的另一种解决方法是,在鼠标按下
时禁用iframe
上的鼠标移动事件:
$('iframe').css('pointer-events', 'none');
然后,在鼠标抬起时重新启用iframe(s)
上的鼠标移动事件:
$('iframe').css('pointer-events', 'auto');
我尝试过一些其他的方法,它们都可以工作,但这似乎是最简单的方法。
感谢:https://www.gyrocode.com/articles/how-to-detect-mousemove-event-over-iframe-element/
MouseEvent.initMouseEvent()
现在已经被弃用,因此@Ozan的回答有些过时。作为他回答所提供内容的替代方案,我现在这样做:
var bubbleIframeMouseMove = function( iframe ){
iframe.contentWindow.addEventListener('mousemove', function( event ) {
var boundingClientRect = iframe.getBoundingClientRect();
var evt = new CustomEvent( 'mousemove', {bubbles: true, cancelable: false})
evt.clientX = event.clientX + boundingClientRect.left;
evt.clientY = event.clientY + boundingClientRect.top;
iframe.dispatchEvent( evt );
});
};
当我设置clientX
和clientY
时,您需要将内容窗口的事件中的任何信息传递给我们将要分派的事件(即,如果您需要传递像screenX
/screenY
这样的内容,请在此操作)。
您的 iframe 中的页面是一个完整的文档。它将消耗所有事件,与其父文档没有直接联系。
您需要从子文档中的 JavaScript 捕获鼠标事件,然后以某种方式将其传递给父级。
<div id="frameOverlay"></div>
<iframe></iframe>
mousemove
。document.getElementById('yourIFrameHere').contentDocument.addEventListener('mousemove', function (event) {
console.log(, event.pageX, event.pageY, event.target.id);
}.bind(this));
<script>
// dispatch events to the iframe from its container
$("body").on('click mouseup mousedown touchend touchstart touchmove mousewheel', function(e) {
var doc = $("#targetFrame")[0].contentWindow.document,
ev = doc.createEvent('Event');
ev.initEvent(e.originalEvent.type, true, false);
for (var key in e.originalEvent) {
// we dont wanna clone target and we are not able to access "private members" of the cloned event.
if (key[0] == key[0].toLowerCase() && $.inArray(key, ['__proto__', 'srcElement', 'target', 'toElement']) == -1) {
ev[key] = e.originalEvent[key];
}
}
doc.dispatchEvent(ev);
});
</script>
<body>
<iframe id="targetFrame" src="eventlistener.html"></iframe>
</body>
var IE = document.all?true:false;
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 0;
// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
} else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
// catch possible negative values in NS4
if (tempX < 0){tempX = 0}
if (tempY < 0){tempY = 0}
// show the position values in the form named Show
// in the text fields named MouseX and MouseY
console.log(tempX, tempY);
return true;
}
window.frames['showTime'].document.onmousemove = getMouseXY;
window.frames['showTime'].document.getElementById("myFrame").contentWindow.onmousemove = getMouseXY;