I have an HTML document that includes 3 iframes representing previous, current, and next pages. I'm trying to enable page swiping (via the jQuery touchswipe plugin), but also let clicks go through to the document inside the iframe. Here's the HTML:
<body>
<div id="pages-wrapper">
<div id="page1" class="page-div previous-page">
<iframe id="frame1" class="page-frame"></iframe>
</div>
<div id="page2" class="page-div active-page">
<iframe id="frame2" class="page-frame"></iframe>
</div>
<div id="page3" class="page-div next-page">
<iframe id="frame3" class="page-frame"></iframe>
</div>
</div>
</body>
When the pages-wrapper element is swiped, either the previous or next page becomes the active-page. Pages are sized 100% and the active page fills the browser's viewport. Everything is within the same domain. The iframe's documents can contain images with imagemaps.
Unfortunately, the iframes capture the mouse events, disabling the swipe capability on the parent pages. As others have suggested, the answer is to overlay a transparent div, use elementFromPoint to locate the target in the iframe's document, and manually dispatch a click event to the target.
I've implemented this using the coverIframes plugin:
$.fn.coverIframes = function(){
$.each($("iframe",this),function(i,v){
var ifr = $(v);
var wr = $("<div id='wr"+new Date().getTime()+i+"' style='z-index: 999999; opacity: 0; position:absolute; width:100%;'></div>");
ifr.before(wr);
wr.height(ifr.height());
wr.click(function(event){
var iframe = ifr.get(0);
var iframeDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
// Find click position (coordinates)
var x = event.offsetX;
var y = event.offsetY;
// Trigger click inside iframe
var link = iframeDoc.elementFromPoint(x, y);
var newEvent = iframeDoc.createEvent('HTMLEvents');
newEvent.initEvent('click', true, true);
link.dispatchEvent(newEvent);
});
})
};
Although this approach works for most elements, it does not do so for an imagemap's areas -- at least within the Chrome browser (ver 32). In these instances, it returns null. In trying to find a workaround, here are 3 questions that came to mind:
Is there a way to pass a click event in the parent directly to the iframe window and let it handle the event exactly as if the user had clicked on the window? (i.e., the window finds the target, and bubbles the event up through the dom's elements.)
Is there any alternative function similar to elementFromPoint that reliably returns the target element?
If I want to manually handle the null result on an area, is there an existing JavaScript function or jQuery plugin that will determine if a given point is within a map's area? (I've seen some functions for a finding a point in a polygon.)