美文网首页
js实现iframe高度的自适应

js实现iframe高度的自适应

作者: 一通 | 来源:发表于2020-03-05 22:10 被阅读0次
    function autoSetIframeHeight(id) {
      try{
            var iframe = document.getElementById(id);
            if(iframe.attachEvent){
              window.setTimeout(function() {
          
      console.log(iframe.contentWindow.document.documentElement.scrollHeight)
             iframe.style.height =  (iframe.contentWindow.document.documentElement.scrollHeight+100)+'px';
          },500)
    
                iframe.attachEvent("onload", function(){
                iframe.contentWindow.onclick = function () {
                  autoSetIframeHeight(id);
                 }
                });
                return;
            }else{
              window.setTimeout(function() {
            console.log(iframe.contentDocument.body.scrollHeight)
             iframe.style.height =  (iframe.contentDocument.body.scrollHeight+100)+'px';
          },500)
                iframe.onload = function(){
                   iframe.contentDocument.onclick = function () {
                  autoSetIframeHeight(id);
               }
                };
                return;                 
            }     
        }catch(e){
            throw new Error('setIframeHeight Error');
        }
    }
    

    相关文章

      网友评论

          本文标题:js实现iframe高度的自适应

          本文链接:https://www.haomeiwen.com/subject/jxllrhtx.html