I am trying to set the iframe height to dynamically adjust based on the window size. The page I am working on is https://www.sierraavalanchecenter.org/weather/weather-tools. I have this iframe embedded in the page.
<iframe frameborder="0" id="wxtable" scrolling="yes" src="https://looper.avalanche.state.co.us/sac/" style="width:95%; min-height: 1275px; "></iframe>
My goal is to:
- Never have the vertical scroll bar on the iframe - horizontal scrolling on mobile is fine and expected.
- Always show all of the content in the iframe
- Not have much white space on the page below the iframe
The iframe height must be taller on mobile than on desktop to achieve this.
If I set a fixed height, I can make it work, but then I either have a lot of whitespace below the iframe on desktop or vertical scrolling within the iframe on mobile.
I tried using the 100vh css property, but that creates a seemingly infinite height.
<div style="height: 100vh; display: flex; flex-direction: column;">
<iframe frameborder="0" id="wxtable" scrolling="yes" src="https://looper.avalanche.state.co.us/sac/" style="flex-grow: 1; border: none; width: 100%;"></iframe>
</div>
I also tried some JavaScript, but it seems like we can't use JavaScript in the generic embed?
<script type="text/javascript">
function resizeIframe(iframe) {
if (iframe.contentWindow && iframe.contentWindow.document.body) {
// Calculate total height
var height = iframe.contentWindow.document.body.scrollHeight;
iframe.style.height = height + "px";
}
}
// Call the function when the iframe loads
document.getElementById('wxtable').onload = function() {
resizeIframe(this);
};
</script>
<script>
window.addEventListener('message', function(event) {
if (event.data.type === 'resize') {
var iframe = document.querySelector('iframe');
iframe.style.height = event.data.value + 'px';
}
}, false);
</script>
I am sure that @rchlfryn, @busbyk, and @stevekuznetsov have way better ideas than me for this!
Thanks
I am trying to set the iframe height to dynamically adjust based on the window size. The page I am working on is https://www.sierraavalanchecenter.org/weather/weather-tools. I have this iframe embedded in the page.
<iframe frameborder="0" id="wxtable" scrolling="yes" src="https://looper.avalanche.state.co.us/sac/" style="width:95%; min-height: 1275px; "></iframe>My goal is to:
The iframe height must be taller on mobile than on desktop to achieve this.
If I set a fixed height, I can make it work, but then I either have a lot of whitespace below the iframe on desktop or vertical scrolling within the iframe on mobile.
I tried using the 100vh css property, but that creates a seemingly infinite height.
I also tried some JavaScript, but it seems like we can't use JavaScript in the generic embed?
I am sure that @rchlfryn, @busbyk, and @stevekuznetsov have way better ideas than me for this!
Thanks