screen width vs visible portion

前端 未结 3 1178
南笙
南笙 2020-12-06 06:58

I have a little problem with JavaScript, to get the screen width we use screen.width which returns the overall screen resolution, is there a command to get the resolution of

3条回答
  •  日久生厌
    2020-12-06 07:16

    function width(){
       return window.innerWidth 
           || document.documentElement.clientWidth 
           || document.body.clientWidth 
           || 0;
    }
    
    function height(){
       return window.innerHeight 
           || document.documentElement.clientHeight 
           || document.body.clientHeight 
           || 0;
    }
    

    Use them to return the height() or width() of the visible window.

    JSFiddle example.

提交回复
热议问题