Advertisement
phpaddict

List viewport for mobile testing in desktop browser

Jul 30th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This piece of code creates a div (in upper left corner) that shows the viewport of current webpage. The viewport size will change when the browser window gets resized. You can insert it anywhere in your code after jQuery has been included.
  3. Note: requires jQuery
  4. */
  5. $(function(){
  6.     function createDiv() {
  7.         var div = $('<div />');
  8.         div.attr({style: 'position: fixed; top: 0; left: 0; border: 1px #aaa solid; background-color: #fff; padding: 2px 4px; color: #444; font-size: 12px;', id: 'viewport'});
  9.         $('body').append(div);
  10.     }
  11.     var _t;
  12.     function windowResized() {
  13.         clearTimeout(_t);
  14.         _t = setTimeout(function() {
  15.             var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  16.             var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  17.             $('#viewport').text('width: '+w+', height: '+h);
  18.         }, 50);
  19.     }
  20.  
  21.     createDiv();
  22.     $(window).resize(windowResized);
  23.     $(window).trigger('resize');
  24. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement