js实现 iframe 宽高自适应跟随浏览器大小改变
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"> 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"; window.onresize = function(){ //监听事件 var main1 = document.getElementById("right"); var width1 = main1.offsetWidth; var height1 = main1.offsetHeight; iframe.style.width = width1 + "px"; iframe.style.height = (height1 - 5) + "px"; } </script>