javascript - jQuery click event does not work when fullscreen iframe in the background? -
jquery click event won't fire when there fullscreen iframe in background :
js :
$( document ).ready(function() { $(document).click(function(){ if ($(this).find('.overlay').is(':visible')) { $('.overlay').hide(); } else { $('.overlay').show(); } }); });
html :
<body> <div class="overlay">overlay</div> <iframe src="iframe.html" name="frame" style="position:fixed; top:0; left:0; right:0; bottom:0; width:100%; height:100%"></iframe> </body>
css :
body { margin: 0; } .overlay { width:200px; height:200px; background-color: green; position:fixed; top:0; left:0; z-index: 10; }
i want hide/ toggle green box when click occurred outside box.
any ideas how can fix this?
to access iframe, .contents()
can used :
$(document).ready(function() { $('[name="frame"]').on('load', function() { $(this).contents().click(function() { if ($('.overlay').is(':visible')) $('.overlay').hide(); else $('.overlay').show(); }); }); });
the .contents() method can used content document of iframe, if iframe on same domain main page.
https://api.jquery.com/contents/
if content on domain, option put element on top of it.
edit - code adjusted, it's important wait iframe load before applying event handler...
Comments
Post a Comment