iframe auto size based on content that changes siz

2019-08-28 02:39发布

I have one page that contains search page iframe . If you try to search for a text it will rarely fit the page.

I don't know what code to use to auto resize iframe based on content changes.

Right now I use this code in iframe :

 <body onload="parent.alertsize(document.body.scrollHeight);">

And this javascript in parent page :

 <script>
function alertsize(pixels){
 pixels+=32;
 document.getElementById('my-iframe').style.height=pixels+"px";
 }
 </script>

1条回答
Bombasti
2楼-- · 2019-08-28 03:11
<script type="text/javascript">
var myHeight = 0;

setInterval(function(){
    if(myHeight != document.body.scrollHeight){
        myHeight = document.body.scrollHeight;
        parent.alertsize(myHeight);
    }
},500);
</script>
查看更多
登录 后发表回答