Piemēram:
<iframe name="Stack" src="http://stackoverflow.com/" width="740"
frameborder="0" scrolling="no" id="iframe"> ...
</iframe>
Es gribu, lai tas varētu pielāgot savu augstumu atbilstoši iekšpusē esošajam saturam, neizmantojot ritināšanu.
Pievienojiet šo sadaļai <head>
:
<script>
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>
Un mainiet savu iframe uz šo:
<iframe src="..." frameborder="0" scrolling="no" onload="resizeIframe(this)" />
Kā atrodams sitepoint discussion.
hjpotter92 ieteikums nedarbojas safari! Esmu veicis nelielu pielāgojumu skriptu, tāpēc tagad tas darbojas arī Safari.
Vienīgā veiktā izmaiņa ir augstuma atiestatīšana uz 0 katrā ielādēšanas reizē, lai dažas pārlūkprogrammas varētu samazināt augstumu.
Pievienojiet to <head>
tagam:
<script type="text/javascript">
function resizeIframe(obj){
obj.style.height = 0;
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>
Un pievienojiet iframe šādu atribūtu onload
, piemēram, šādi
<iframe onload='resizeIframe(this)'></iframe>
function autoResize(id){
var newheight;
var newwidth;
if(document.getElementById){
newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
newwidth=document.getElementById(id).contentWindow.document.body.scrollWidth;
}
document.getElementById(id).height=(newheight) + "px";
document.getElementById(id).width=(newwidth) + "px";
}
pievienojiet to savam iframe: pievienojiet: onload="autoResize('youriframeid')"