iframe自适应内容高度

<!--
 * @Description: 描述
 * @Version: 1.0
 * @Autor: Nanke_南柯
 * @Date: 2020-11-03 14:09:04
 * @LastEditors: Nanke_南柯
 * @LastEditTime: 2020-11-03 14:09:05
-->
<!DOCTYPE html>
<html lang="sv">

<head>
    <meta charset="utf-8" />
    <title>Iframe height demo</title>
    <style media="screen,print">
        #body {
             70em;
            max- 100%;
            margin: 0 auto;
        }

        iframe {
             100%;
            margin: 0 0 1em;
            border: 0;
        }
    </style>
    <script>
        /*
         * When the iframe is on a different subdomain, uncomment the following line
         * and change "example.com" to your domain.
         */
        // document.domain = "example.com";
        function setIframeHeight(iframe) {
            if (iframe) {
                var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
                if (iframeWin.document.body) {
                    iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
                    console.log(iframeWin.document.documentElement.scrollHeight , iframeWin.document.body.scrollHeight);
                }
            }
        };
    </script>
</head>

<body>
    <div id="body">
        <h1>Iframe height demo</h1>
        <h2><code>iframe</code> <strong>with</strong> height adjustment</h2>
        <iframe src="iframe高度自适应-src.html"
            frameborder="0"
            id="external-frame" onload="setIframeHeight(this)"></iframe>
    </div>
</body>

</html>
原文地址:https://www.cnblogs.com/NanKe-Studying/p/13920003.html