javascript – 如何将iframe的高度和宽度设置为100%

前端之家收集整理的这篇文章主要介绍了javascript – 如何将iframe的高度和宽度设置为100%前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要做以下事情:

>我有一个标题内容是70px,一个iframe与一个包装.
高度& iframe的宽度必须设置为100%
滚动,除了为身体的滚动
内容加载,内容大小更多.
> iframe的内容是来自同一个域的另一个HTML页面.
iframe还需要根据响应的HTML进行扩展
页.

可以帮忙吗

我不能使用的东西

位置:固定;(主要是由于我用于侧栏的脚本

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>Bus Management System</title>
  6. <!-- Viewport Meta tag to prevent iPhone from scaling our page -->
  7. <Meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  8.  
  9.  
  10. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  11. <script>
  12.  
  13. $(function(){
  14.  
  15. var iFrames = $('iframe');
  16.  
  17. function iResize() {
  18.  
  19. for (var i = 0,j = iFrames.length; i < j; i++) {
  20. iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';}
  21. }
  22.  
  23. if ($.browser.safari || $.browser.opera) {
  24.  
  25. iFrames.load(function(){
  26. setTimeout(iResize,0);
  27. });
  28.  
  29. for (var i = 0,j = iFrames.length; i < j; i++) {
  30. var iSource = iFrames[i].src;
  31. iFrames[i].src = '';
  32. iFrames[i].src = iSource;
  33. }
  34.  
  35. } else {
  36. iFrames.load(function() {
  37. this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
  38. });
  39. }
  40.  
  41. });
  42. </script>
  43.  
  44.  
  45. </head>
  46.  
  47. <style type="text/css" media="all">
  48. html,body,iframe {width:100%; height:100%;Box-sizing:border-Box; margin:0; padding:0; border:0;}
  49. body {border:4px solid green;}
  50. iframe {border:6px solid red;width:100%; height:100%;display:block;}
  51. #wrapper {border:2px solid blue; width:100%; height:100%;}
  52.  
  53. </style>
  54. <body>
  55. <div style="height:50px; background-color:#000; color:#fff;">Header</div>
  56.  
  57. <iframe src="http://www.google.com" style="width:100%; height:100%;" onLoad="calcHeight();"></iframe>
  58.  
  59. </body>
  60. </html>

解决方法

CSS仅适用于100%宽度和高度的响应

HTML

  1. <div class="container">
  2. <div class="h_iframe">
  3. <iframe src="//www.youtube.com/embed/9KunP3sZyI0" frameborder="0" allowfullscreen></iframe>
  4. </div>
  5. </div>

CSS

  1. html,body {
  2. height:100%;
  3. }
  4. .h_iframe iframe {
  5. position:absolute;
  6. top:0;
  7. left:0;
  8. width:100%;
  9. height:100%;
  10. }

DEMO

没有位置:绝对

CSS

  1. html,body {height:100%;}
  2. .h_iframe iframe {width:100%; height:100%;}
  3. .h_iframe {
  4. height: 100%;
  5. }

DEMO 2

最后这是破解

现代浏览器现在支持

  1. width: calc(100% - 70px);

CSS

  1. html,body {
  2. height:100%;
  3. margin-top:0;
  4. margin-bottom:0;
  5. }
  6. .h_iframe iframe {
  7. width:100%;
  8. height:calc(100% - 75px);
  9. }
  10. .h_iframe {
  11. height: 100%;
  12. }
  13. .element{
  14. width:100%;
  15. height:70px;
  16. background:red;
  17. }

HTML

  1. <div class="container">
  2. <div class="element">here it goes</div>
  3. <div class="h_iframe">
  4. <iframe src="//www.youtube.com/embed/9KunP3sZyI0" frameborder="0" allowfullscreen></iframe>
  5. </div>
  6. </div>

Final DEMO

猜你在找的JavaScript相关文章