js实现 iframe 宽高自适应

css

<style type="text/css" media="screen">

.box{
  width: 80%;
  height: 500px;
  margin: 0 auto;
}
.left{
  width: 20%;
  height: 500px;
  float: left;
  border: 1px solid red;
}
#right{
  width: 79%;
  height: 500px;
  border: 1px solid red;
  float: left;
}
</style>

 

html

<div class="box">
  <div class="left">
    <a href="https://www.taobao.com/" title="" target="iframe_a">淘宝</a><br />
    <a href="https://www.jd.com/" title="" target="iframe_a">京东</a>
  </div>
  <div id="right">
    <iframe id="iframe" src="" name="iframe_a" frameborder="1" ></iframe>
  </div>
</div>

 

js

<script type="text/javascript">
  

<script type="text/javascript">
  var main = document.getElementById("right");
  var iframe = document.getElementById("iframe");
  var width = main.offsetWidth;
  var height = main.offsetHeight;
  iframe.style.width = width + "px";
  iframe.style.height = (height - 5) + "px";
</script>


</script>

posted @ 2019-09-19 00:58  &权  阅读(538)  评论(0编辑  收藏  举报