Iframe 100% height inside body with padding

前端 未结 4 587
梦如初夏
梦如初夏 2020-12-05 14:52

I have an iframe in my HTML document and I\'m having a bit of trouble.

I also have a URL bar (fixed position element) at the top of the page that should stay with th

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 15:26

    To build on top of bobince's answer:

    Erik Arvidsson came up with a way to (kinda, sorta) add box-sizing support to IE6/IE7. However, his solution doesn't support units other than px. Like you, I needed a percentage height, so I added support for percents.

    Once you've downloaded and unzipped the zip file, open boxsizing.htc and replace the following border/padding functions:

    /* border width getters */
    function getBorderWidth(el, sSide) {
        if (el.currentStyle["border" + sSide + "Style"] == "none")
            return 0;
        var n = parseInt(el.currentStyle["border" + sSide + "Width"]);
        return n || 0;
    }
    
    function getBorderLeftWidth()   { return getBorderWidth((arguments.length > 0 ? arguments[0] : element), "Left"); }
    function getBorderRightWidth()  { return getBorderWidth((arguments.length > 0 ? arguments[0] : element), "Right"); }
    function getBorderTopWidth()    { return getBorderWidth((arguments.length > 0 ? arguments[0] : element), "Top"); }
    function getBorderBottomWidth() { return getBorderWidth((arguments.length > 0 ? arguments[0] : element), "Bottom"); }
    /* end border width getters */
    
    /* padding getters */
    function getPadding(el, sSide) {
        var n = parseInt(el.currentStyle["padding" + sSide]);
        return n || 0;
    }
    
    function getPaddingLeft()   { return getPadding((arguments.length > 0 ? arguments[0] : element), "Left"); }
    function getPaddingRight()  { return getPadding((arguments.length > 0 ? arguments[0] : element), "Right"); }
    function getPaddingTop()    { return getPadding((arguments.length > 0 ? arguments[0] : element), "Top"); }
    function getPaddingBottom() { return getPadding((arguments.length > 0 ? arguments[0] : element), "Bottom"); }
    /* end padding getters */
    

    Then replace updateBorderBoxWidth and updateBorderBoxHeight with the following:

    function updateBorderBoxWidth() {
        element.runtimeStyle.width = "";
        if (getDocumentBoxSizing() == getBoxSizing())
            return;
        var csw = element.currentStyle.width;
        var w = null;
        if (csw != "auto" && csw.indexOf("px") != -1) {
            w = parseInt(csw);
        } else if (csw != "auto" && csw.indexOf("%") != -1) {
            var origDisplay = element.runtimeStyle.display;
            element.runtimeStyle.display = "none";
    
            w = Math.max(0, (parseInt(element.parentNode.clientWidth) - (
                    getBorderLeftWidth(element.parentNode)
                     + getPaddingLeft(element.parentNode)
                     + getPaddingRight(element.parentNode)
                     + getBorderRightWidth(element.parentNode)
            )) * (parseInt(csw) / 100));
    
            element.runtimeStyle.display = origDisplay;
        }
        if (w !== null) {
            if (getBoxSizing() == "border-box") {
                setBorderBoxWidth(w);
            } else {
                setContentBoxWidth(w);
            }
        }
    }
    
    function updateBorderBoxHeight() {
        element.runtimeStyle.height = "";   
        if (getDocumentBoxSizing() == getBoxSizing())
            return;
        var csh = element.currentStyle.height;
        var h = null;
        if (csh != "auto" && csh.indexOf("px") != -1) {
            h = parseInt(csh);
        } else if (csh != "auto" && csh.indexOf("%") != -1) {
            var origDisplay = element.runtimeStyle.display;
            element.runtimeStyle.display = "none";
    
            h = Math.max(0, (parseInt(element.parentNode.clientHeight) - (
                    getBorderTopWidth(element.parentNode)
                    + getPaddingTop(element.parentNode)
                    + getPaddingBottom(element.parentNode)
                    + getBorderBottomWidth(element.parentNode)
            )) * (parseInt(csh) / 100));
    
            element.runtimeStyle.display = origDisplay;
        }
        if (h !== null) {
            if (getBoxSizing() == "border-box") {
                setBorderBoxHeight(h);
            } else {
                setContentBoxHeight(h);
            }
        }
    }
    

    Then just use the file as you would otherwise:

    .border-box {
        behavior:           url("boxsizing.htc");
        box-sizing:         border-box;
        -moz-box-sizing:    border-box;
        -webkit-box-sizing: border-box;
    }
    

    Here's a pretty thorough test I put together while developing my modifications:

    
    
        
            box-sizing: border-box;
            
            
        
        
            
            

    Nested Header - 40px;

    Float - 100px;

    • This element should never scroll.

    Inner Head - 30px;

    The fourth square should look just like the other three:


    Inner Content - fluid

    • The top of the scrollbar should be covered by the “Inner Head” element.
    • The bottom of the scrollbar should be visible without having to scroll “Inner Head” out of view.

    Document Compat Mode:


    Notes

    • In IE6 and IE7 (and possibly IE8; untested), you'll notice a slight shift of contents that have box-sizing set to border-box. This is the amount of time it takes for box-sizing.htc to finish downloading.
    • This workaround is not live. Anything that causes a reflow or repaint will not currently trigger an update to widths and heights of border-box elements.
    • See http://webfx.eae.net/dhtml/boxsizing/boxsizing.html for the original solution to the IE6/IE7 border-box problem. box-sizing.htc has been modified to allow for percentage widths and heights.
    • To see what this example should look like without the use of box-sizing.htc, view it in Firefox or IE8.

    DOM Update Test
    Content box fill
    Content box fill
    Border box fill
    Border box fill
    Default box fill
    Default box fill
    width: 130px
    height: 100%
    bottom should be cut off
    width: 120px
    height: 100%
    bottom should be cut off
    width: 100px
    height: 100%
    bottom should be visible
    width: 100px
    height: 100%
    bottom should be visible
    width: 130px
    height: 100%
    bottom should be cut off
    width: 120px
    height: 100%
    bottom should be cut off

提交回复
热议问题