【问题标题】:javascript mouseout triggers on interactive iframe交互式 iframe 上的 javascript mouseout 触发器
【发布时间】:2017-04-26 11:32:22
【问题描述】:
我有一个虚拟游览 iframe,当我浏览 iframe 的控件时,jquery mouseout 事件会随意触发。
当我只在 iframe 中导航控件时,为什么会触发 jquery mouseout 事件。
当我只导航 iframe 控件时,我不想触发 mouseout 触发器,因为我有另一个 iframe mouseout 函数。
这是example`
$("iframe").mouseout(function() {
console.log("mouseout!");
})
【问题讨论】:
标签:
javascript
jquery
html
iframe
web-applications
【解决方案1】:
下面的代码可以帮助你,
//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);