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

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

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

19条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 06:12

    Sometimes you need to see the width/height changes while resizing the window and inner content.

    For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates.

    It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:

    • use it on an actual site
    • use it for testing mobile/responsive views


    Tested on: Chrome 40, IE11, but it is highly possible to work on other/older browsers too ... :)

      function gebID(id){ return document.getElementById(id); }
      function gebTN(tagName, parentEl){ 
         if( typeof parentEl == "undefined" ) var parentEl = document;
         return parentEl.getElementsByTagName(tagName);
      }
      function setStyleToTags(parentEl, tagName, styleString){
        var tags = gebTN(tagName, parentEl);
        for( var i = 0; iscreenx"
          +"windowx"
          +"document
    .documentElementx" +"document.bodyx" ; gebTN("body")[0].appendChild( table ); table.setAttribute( 'style', "border: 2px solid black !important; position: fixed !important;" +"left: 50% !important; top: 0px !important; padding:10px !important;" +"width: 150px !important; font-size:18px; !important" +"white-space: pre !important; font-family: monospace !important;" +"z-index: 9999 !important;background: white !important;" ); setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;"); setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;"); table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' ); setInterval( testSizes, 200 );

    EDIT: Now styles are applied only to logger table element - not to all tables - also this is a jQuery-free solution :)

提交回复
热议问题