How can I scale the content of an iframe?

前端 未结 17 2059
后悔当初
后悔当初 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:23

    I think you can do this by calculating the height and width you want with javascript (via document.body.clientWidth etc.) and then injecting the iframe into your HTML like this:

    var element = document.getElementById("myid");
    element.innerHTML += "<iframe src='http://www.google.com' height='200' width='" + document.body.clientWidth * 0.8 + "'/>";
    

    I didn't test this in IE6 but it seems to work with the good ones :)

    0 讨论(0)
  • 2020-11-22 12:24

    So probably not the best solution, but seems to work OK.

    <IFRAME ID=myframe SRC=.... ></IFRAME>
    
    <SCRIPT>
        window.onload = function(){document.getElementById('myframe').contentWindow.document.body.style = 'zoom:50%;';};
    </SCRIPT>
    

    Obviously not trying to fix the parent, just adding the "zoom:50%" style to the body of the child with a bit of javascript.

    Maybe could set the style of the "HTML" element, but didn't try that.

    0 讨论(0)
  • 2020-11-22 12:26

    The #wrap #frame solution works fine, as long as the numbers in #wrap is #frame times the scale factor. It shows only that part of the scaled down frame. You can see it here scaling down websites and putting it into a pinterest like form (with the woodmark jQuery plugin):

    http://www.genautica.com/sandbox/woodmark-index.html

    0 讨论(0)
  • 2020-11-22 12:30

    I do not think HTML has such functionality. The only thing I can imagine would do the trick is to do some server-side processing. Perhaps you could get an image snapshot of the webpage you want to serve, scale it on the server and serve it to the client. This would be a non-interactive page however. (maybe an imagemap could have the link, but still.)

    Another idea would be to have a server-side component that would alter the HTML. SOrt of like the firefox 2.0 zoom feature. this of course is not perfect zooming, but is better than nothing.

    Other than that, I am out of ideas.

    0 讨论(0)
  • 2020-11-22 12:32

    You don't need to wrap the iframe with an additional tag. Just make sure you increase the width and height of the iframe by the same amount you scale down the iframe.

    e.g. to scale the iframe content to 80% :

    #frame { /* Example size! */
        height: 400px; /* original height */
        width: 100%; /* original width */
    }
    #frame {
        height: 500px; /* new height (400 * (1/0.8) ) */
        width: 125%; /* new width (100 * (1/0.8) )*/
    
        transform: scale(0.8); 
        transform-origin: 0 0;
    }
    

    Basically, to get the same size iframe you need to scale the dimensions.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题