How can I scale the content of an iframe?

前端 未结 17 2292
后悔当初
后悔当初 2020-11-22 11:46

How can I scale the content of an iframe (in my example it is an HTML page, and is not a popup) in a page of my web site?

For example, I want to display the content

17条回答
  •  爱一瞬间的悲伤
    2020-11-22 12:32

    If you want the iframe and its contents to scale when the window resizes, you can set the following to the window's resize event as well as the iframes onload event.

    function()
    {
        var _wrapWidth=$('#wrap').width();
        var _frameWidth=$($('#frame')[0].contentDocument).width();
    
        if(!this.contentLoaded)
          this.initialWidth=_frameWidth;
        this.contentLoaded=true;
        var frame=$('#frame')[0];
    
        var percent=_wrapWidth/this.initialWidth;
    
        frame.style.width=100.0/percent+"%";
        frame.style.height=100.0/percent+"%";
    
        frame.style.zoom=percent;
        frame.style.webkitTransform='scale('+percent+')';
        frame.style.webkitTransformOrigin='top left';
        frame.style.MozTransform='scale('+percent+')';
        frame.style.MozTransformOrigin='top left';
        frame.style.oTransform='scale('+percent+')';
        frame.style.oTransformOrigin='top left';
      };
    

    This will make the iframe and its content scale to 100% width of the wrap div (or whatever percent you want). As an added bonus, you don't have to set the css of the frame to hard coded values since they'll all be set dynamically, you'll just need to worry about how you want the wrap div to display.

    I've tested this and it works on chrome, IE11, and firefox.

提交回复
热议问题