<div class="iframe">
<iframe src="https://www.rehau.com" frameborder="0" scrolling="no" height="400" allowfullscreen></iframe>
</div>
<div class="iframe">
<iframe src="{{{src}}}" frameborder="0" scrolling="no" height="{{height}}" allowfullscreen></iframe>
</div>
{
"src": "https://www.rehau.com",
"height": "400"
}
.iframe {
display: block;
overflow: hidden;
> iframe {
width: 100%;
border: 0;
}
}
Full iframe height will be set only if iframe source page sends its height data with parent.postMessage() method.
Example script which can be included in source page for responsive iframe height adjustments provided below:
(function () {
function sendHeight() {
if (!parent.postMessage) return;
var height = getHeight();
parent.postMessage(height, '*');
}
function getHeight() {
var el = document.createElement('div');
el.style.height="0px !important";
el.style.clear="both";
document.body.appendChild(el);
var offset = el.offsetTop;
el.parentNode.removeChild(el);
return offset;
}
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var resizeEvent = eventMethod === "attachEvent" ? "onresize" : "resize";
var timeout = false;
eventer(resizeEvent, function() {
timeout && clearTimeout(timeout);
timeout = setTimeout(sendHeight, 150); //150ms debounce time
});
sendHeight();
})();