iframe:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/iframe
使用:onload="this.height=this.contentWindow.document.documentElement.scrollHeight"
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>自适应高度</h1> <iframe srcdoc='<div style="height: 400px;width: 400px;background: #ddd;"></div>' frameborder="1" scrolling="no" width="100%" onload="this.height=this.contentWindow.document.documentElement.scrollHeight"></iframe> <h1>非自适应高度</h1> <iframe srcdoc='<div style="height: 400px;width: 400px;background: #ddd;"></div>' frameborder="1" scrolling="no" width="100%"></iframe> </body> </html> |
定时改变 iframe 高度:
1 2 3 |
setInterval(()=>{ document.querySelector('iframe').height = window.frames[0].document.documentElement.scrollHeight; }, 200); |
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>自适应高度</h1> <iframe srcdoc='<button onclick="this.parentNode.appendChild(this.cloneNode())" style="width: 100px; height: 100px;display: block;"></button>' frameborder="1" scrolling="no" width="100%" onload="this.height=this.contentWindow.document.documentElement.scrollHeight"></iframe> <h1>非自适应高度</h1> <iframe srcdoc='<button onclick="this.parentNode.appendChild(this.cloneNode())" style="width: 100px; height: 100px;display: block;"></button>' frameborder="1" scrolling="no" width="100%"></iframe> <script> setInterval(()=>{ document.querySelector('iframe').height = window.frames[0].document.documentElement.scrollHeight; }, 200); </script> </body> </html> |
from:https://www.cnblogs.com/jffun-blog/p/9774121.html