Visible window height instead of $(window).height();

前端 未结 3 1313
梦谈多话
梦谈多话 2021-02-03 17:34

Is there any way to get the visible height of the whole page from inside an iframe, $(window).height() gives me the iframes height?

3条回答
  •  别跟我提以往
    2021-02-03 18:30

    I had cause to address a similiar issue today, because in FF screen.height and window.innerHeight return the same value, and of course my first response was to check for solutions on SO. In the end this is how I addressed the matter, and I'm posting the longwinded version here for posterity only...

    function visibleWindowHeight( callback ) {
        /* create temporary element 'tmp' */
        var vpHeight,
            tmp = document.createElement('div');
    
        tmp.id = "vp_height_px";
    
        /* tmp height = viewport height (100vh) */
        tmp.setAttribute("style", "position:absolute;" +
            "top:0;" +
            "left:-1px;" +
            "width:1px;" +
            "height:100vh;");
    
        /* add tmp to page */
        document.body.appendChild(tmp);
    
        /* get tmp height in px */
        vpHeight = document.getElementById("vp_height_px").clientHeight;
    
        /* clean up */
        document.body.removeChild(tmp);
    
        /* pass value to callback and continue */
        callback( vpHeight );
    }
    
    document.addEventListener('DOMContentLoaded', function() {
    
        visibleWindowHeight( function( visibleHeight ) {
    
            console.log("visibleHeight:", visibleHeight);
            /*
                ... continue etc etc  ...
            */
        });
    
    }, !1);
    

    It might help someone, sometime.

提交回复
热议问题