Resize iframe to content with Jquery

前端 未结 6 2177
孤街浪徒
孤街浪徒 2020-12-03 18:08

I\'m trying to resize an iframe dynamicly to fit its content. To do so I have a piece of code:

$(\"#IframeId\").height($(\"#IframeId\").contents().find(\"htm         


        
6条回答
  •  离开以前
    2020-12-03 18:46

    As the answer to the question use an already outdated jquery (load has been deprecated and replaced with .on('load',function(){}), below is the latest code for the answer in the question.

    Note that I use the scrollHeight and scrollWidth, which I think will load much nicer than using Height and Width like the answer provided. It will totally fit, without scroll anymore.

    $("#dreport_frame").on('load',function(){
    
      var h = $('#dreport_frame').contents().find("body").prop('scrollHeight');
      var w = $('#dreport_frame').contents().find("body").prop('scrollWidth');
    
      $('#dreport_frame').height(h);
      $('#dreport_frame').width(w);
    })
    

提交回复
热议问题