admin管理员组文章数量:1410737
I have found threads such as How to dynamically resize iFrame in Firefox? which talk about sizing an iframe based on the remote content; however, I have not found a way (in Firefox) to set the height of the iframe based on the window size.
Other browsers are able to use .body.scrollHeight
but it appears that Firefox can't use that. See... /
To see the iframe in action with the auto-sizing, view this page... /
It works in browsers such as Chrome but not Firefox
I have found threads such as How to dynamically resize iFrame in Firefox? which talk about sizing an iframe based on the remote content; however, I have not found a way (in Firefox) to set the height of the iframe based on the window size.
Other browsers are able to use .body.scrollHeight
but it appears that Firefox can't use that. See... http://jsfiddle/gjrowe/X63KR/
To see the iframe in action with the auto-sizing, view this page... http://jsfiddle/gjrowe/y2WCP/
It works in browsers such as Chrome but not Firefox
Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Apr 10, 2013 at 18:58 G-JG-J 1,0682 gold badges18 silver badges32 bronze badges 1- I know its an old thread but also take a look at stackoverflow./questions/153152/… that may be another way to deal with it. – Kendrick Commented Apr 26, 2014 at 2:25
2 Answers
Reset to default 3Here is what I did to have a cross-browser fix...
I added this javascript function...
function getDocHeight() {
var D = document;
return Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}
I then changed...
var content_height=document.body.scrollHeight-100;
to...
var content_height=getDocHeight()-100;
You can see it in action at http://jsfiddle/y2WCP/9/
i don't know if you want to use jQuery, but with jQuery i think i fixed your problem..
// Header is 50px and footer is 50px; therefore, 100px is of screen height is used
// Define content_height and consider the 100px which has already been used
var content_height=document.body.scrollHeight-100;
//alert(content_height);
content_height = $(window).height() -110;
//alert(content_height);
// Set iframe height
document.getElementById('pdf_frame').style.height=content_height+"px";
// Reset iframe height after window resize
$(function(){
$(window).resize(function(){
content_height = $(window).height()-110;
//var content_height=document.body.scrollHeight-100;
document.getElementById('pdf_frame').style.height=content_height+"px";
});
});
jsFiddle link
本文标签: javascriptDynamically define iframe height based on window size (NOT CONTENT)Stack Overflow
版权声明:本文标题:javascript - Dynamically define iframe height based on window size (NOT CONTENT) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744943883a2633658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论