Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mousemove when over an iframe?

I have an iframe that takes up the entire window (100% wide, 100% high), and I need the main window to be able to detect when the mouse has been moved.

Already tried an onMouseMove attribute on the iframe and it obviously didn't work. Also tried wrapping the iframe in a div like so:

<div onmousemove="alert('justfortesting');"><iframe src="foo.bar"></iframe></div> 

.. and it didn't work. Any suggestions?

like image 492
Charles Zink Avatar asked Apr 13 '11 06:04

Charles Zink


People also ask

Do iframe events bubble?

The iframe is a separate document tree, and so events that bubble through its tree terminate at the root of the iframe's document and do not travel across the boundary into the host document. This upward propagation will continue up to and including the Document.

Can browser detect mouse movement?

JavaScript is implemented as part of a Web browser and is supported by all the major web browsers, including Internet Explorer, Firefox and Safari. Therefore, using this language, Web developers can track user's mouse movements simply by entering lines of code on a page.

How do I turn off mouse click on iframe?

One of the simple ways to do this is to just disable pointer events inside iframe, using CSS. Here is a sample CSS configuration for this purpose. We use pointer-events property for this purpose, and set it to none value.

Which event is used when mouse moves over the element?

The mouseover event is fired at an Element when a pointing device (such as a mouse or trackpad) is used to move the cursor onto the element or one of its child elements.


2 Answers

If your target isn't Opera 9 or lower and IE 9 or lower you can use css attribute pointer-events: none.

I found it the best way just to ignore iframe. I add class with this attribute to iframe in onMouseDown event and remove in onMouseUp event.

Works perfect for me.

like image 176
Łukasz Jagodziński Avatar answered Sep 25 '22 14:09

Łukasz Jagodziński


Iframes capture mouse events, but you can transfer the events to the parent scope if the cross-domain policy is satisfied. Here's how:

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

You can now listen for mousemove on the iframe element or any of its parent elements -- the event will bubble up as you would expect.

This is compatible with modern browsers. If you need it to work with IE8 and below, you'll need to use the IE-specific replacements of createEvent, initMouseEvent, and dispatchEvent.

like image 24
Ozan Avatar answered Sep 23 '22 14:09

Ozan