<!DOCTYPE html> <html lang="sv"> <head> <meta charset="utf-8" /> <title>Iframe height demo</title> <style media="screen,print"> #body { width: 70em; max-width: 100%; margin: 0 auto; } iframe { width: 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>
浏览器所有内容高度: 浏览器整个框架的高度,包括滚动条卷去部分+可视部分+底部隐藏部分的高度总和。
浏览器滚动部分高度: 滚动条卷去部分高度即可视顶端距离整个对象顶端的高度。
document.documentElement.scrollHeight——浏览器所有内容高度
document.body.scrollHeight——浏览器所有内容高度
document.documentElement.scrollTop——浏览器滚动部分高度
document.body.scrollTop——始终为0
document.documentElement.clientHeight——浏览器可视部分高度
document.body.clientHeight——浏览器所有内容高度
document.documentElement.scrollHeight——浏览器所有内容高度
document.body.scrollHeight——浏览器所有内容高度
document.documentElement.scrollTop——浏览器滚动部分高度
document.body.scrollTop——始终为0
document.documentElement.clientHeight——浏览器可视部分高度
document.body.clientHeight——浏览器所有内容高度
document.documentElement.scrollHeight——浏览器所有内容高度
document.body.scrollHeight——浏览器所有内容高度
document.documentElement.scrollTop——始终为0
document.body.scrollTop——浏览器滚动部分高度
document.documentElement.clientHeight——浏览器可视部分高度
document.body.clientHeight——浏览器所有内容高度
——————————————————————————————————————————————
document.documentElement.scrollHeight——浏览器可视部分高度
document.body.scrollHeight——浏览器所有内容高度
document.documentElement.scrollTop——始终为0
document.body.scrollTop——浏览器滚动部分高度
document.documentElement.clientHeight——始终为0
document.body.clientHeight——浏览器可视部分高度
document.documentElement.scrollHeight——浏览器可视部分高度
document.body.scrollHeight——浏览器所有内容高度
document.documentElement.scrollTop——始终为0
document.body.scrollTop——浏览器滚动部分高度
document.documentElement.clientHeight——浏览器所有内容高度
document.body.clientHeight——浏览器可视部分高度
document.documentElement.scrollHeight——浏览器可视部分高度
document.body.scrollHeight——浏览器所有内容高度
document.documentElement.scrollTop——始终为0
document.body.scrollTop——浏览器滚动部分高度
document.documentElement.clientHeight——浏览器所有内容高度
document.body.clientHeight——浏览器可视部分高度
从上面的情况可以得出
1、document.documentElement.scrollTop和document.body.scrollTop始终有一个为0,所以可以用这两个的和来求scrollTop
2、scrollHeight、clientHeight 在DTD已声明的情况下用documentElement,未声明的情况下用body
PS:可以使用 document.compatMode 可以用来判断是否声明了DTD,得值进行判断。
我们则没有使用document.compatMode来判断,而是直接
var bodyHeight=document.body.scrollHeight==0?document.documentElement.scrollHeight:document.body.scrollHeight;
var height = bodyHeight -70;
contentDocument 属性能够以 HTML 对象来返回 iframe 中的文档。
可以通过所有标准的 DOM 方法来处理被返回的对象。
所有主要浏览器都支持 contentWindow 属性
https://www.runoob.com/try/try.php?filename=tryjsref_iframe_contentdocument
原文:https://www.cnblogs.com/websmile/p/12889310.html