Adjust width and height of iframe to fit with content in it

前端 未结 30 2387
旧时难觅i
旧时难觅i 2020-11-22 02:09

I need a solution for auto-adjusting the width and height of an iframe to barely fit its content. The point is that t

30条回答
  •  被撕碎了的回忆
    2020-11-22 02:45

    I know the post is old, but I believe this is yet another way to do it. I just implemented on my code. Works perfectly both on page load and on page resize:

    var videoHeight;
    var videoWidth;
    var iframeHeight;
    var iframeWidth;
    
    function resizeIframe(){
        videoHeight = $('.video-container').height();//iframe parent div's height
        videoWidth = $('.video-container').width();//iframe parent div's width
    
        iframeHeight = $('.youtubeFrames').height(videoHeight);//iframe's height
        iframeWidth = $('.youtubeFrames').width(videoWidth);//iframe's width
    }
    resizeIframe();
    
    
    $(window).on('resize', function(){
        resizeIframe();
    });
    

提交回复
热议问题