Get the size of the screen, current web page and browser window

前端 未结 19 2375
面向向阳花
面向向阳花 2020-11-21 05:29

How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight,

19条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 06:22

    here is my solution!

    // innerWidth
    const screen_viewport_inner = () => {
        let w = window,
            i = `inner`;
        if (!(`innerWidth` in window)) {
            i = `client`;
            w = document.documentElement || document.body;
        }
        return {
            width: w[`${i}Width`],
            height: w[`${i}Height`]
        }
    };
    
    
    // outerWidth
    const screen_viewport_outer = () => {
        let w = window,
            o = `outer`;
        if (!(`outerWidth` in window)) {
            o = `client`;
            w = document.documentElement || document.body;
        }
        return {
            width: w[`${o}Width`],
            height: w[`${o}Height`]
        }
    };
    
    // style
    const console_color = `
        color: rgba(0,255,0,0.7);
        font-size: 1.5rem;
        border: 1px solid red;
    `;
    
    
    
    // testing
    const test = () => {
        let i_obj = screen_viewport_inner();
        console.log(`%c screen_viewport_inner = \n`, console_color, JSON.stringify(i_obj, null, 4));
        let o_obj = screen_viewport_outer();
        console.log(`%c screen_viewport_outer = \n`, console_color, JSON.stringify(o_obj, null, 4));
    };
    
    // IIFE
    (() => {
        test();
    })();

提交回复
热议问题