Last active
January 3, 2019 21:42
-
-
Save jgmuchiri/06c55465bf57c776687e13854027429a to your computer and use it in GitHub Desktop.
Resizes iframe if window height resizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<iframe | |
src="your-url-here" | |
id="my_iframe" | |
style="width: 100%;" | |
border="0" | |
margin="0" padding="0" | |
frameborder="0" scrolling="no" ></iframe> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($, window, undefined){"use strict"; | |
var frameId = "my_iframe"; | |
var minHeight = 500; | |
var iframeHeight = 0; | |
var threshold = 5; | |
var frame = $("#"+frameId); | |
$(window).on("message", function(e) { | |
var data = e.originalEvent.data; | |
if (!data.match(/^HEIGHT:/)) {return true;} | |
var h = parseInt(data.substring("HEIGHT:".length), 10); | |
if (h < minHeight) h = minHeight; | |
if (Math.abs(iframeHeight - h) > threshold) { | |
iframeHeight = h; | |
frame.height(iframeHeight); | |
} | |
}); | |
})(this.jQuery, this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment