javascript mouseout 在交互式 iframe 上触发

标签 javascript jquery html iframe web-applications

我有一个虚拟旅游 iframe,当我导航 iframe 的控件时,jquery mouseout 事件会偶然触发。
当我仅在 iframe 中导航控件时,为什么会触发 jquery mouseout 事件。

当我只导航 iframe 控件时,我不希望触发 mouseout,因为我还有另一个用于 iframe mouseout 的函数。

这是example `

$("iframe").mouseout(function() {
  console.log("mouseout!");
})

最佳答案

下面的代码可以帮助你,

//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);

关于javascript mouseout 在交互式 iframe 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43632998/

相关文章:

javascript - 如何在本地运行 Parse 后台作业

javascript - iggrid 添加新行事件

javascript - Highcharts 突出显示两条线之间的区域

javascript - 我可以简化这个 jquery 代码吗?

jquery - 即使单击下一个按钮,我也无法移至下一页

javascript - 猫头鹰轮播视频没有响应地调整大小

javascript - 为什么使用 DOMNodeInserted 在控制台中正确显示元素的 id 时未定义?

javascript - Jquery 调用 :first 的替代方法

jquery - 从 jquery 访问内容页面元素

html - HTML 中的 IBM 扩展 ASCII 字符