viewportsizemonitor.html 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.dom.ViewportSizeMonitor</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.dom');
  13. goog.require('goog.events');
  14. goog.require('goog.events.EventType');
  15. goog.require('goog.math.Size');
  16. goog.require('goog.style');
  17. goog.require('goog.dom.ViewportSizeMonitor');
  18. </script>
  19. <link rel="stylesheet" href="css/demo.css">
  20. <style>
  21. html, body {
  22. margin: 0;
  23. border: 0;
  24. padding: 10px;
  25. overflow: hidden;
  26. }
  27. div#demo {
  28. position: relative;
  29. top: 0;
  30. left: 0;
  31. margin: 0;
  32. border: 0;
  33. padding: 0;
  34. }
  35. span#current_size {
  36. font: bold 9pt Arial, Helvetica, sans-serif;
  37. color: #333;
  38. white-space: nowrap;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h1>goog.dom.ViewportSizeMonitor</h1>
  44. <div id="demo">
  45. Current Size: <span id="current_size">Loading...</span>
  46. </div>
  47. <script>
  48. // Takes a goog.math.Size object containing the viewport size, and updates
  49. // the UI accordingly.
  50. function updateUi(size) {
  51. goog.style.setSize(goog.dom.getElement('demo'), size);
  52. goog.dom.setTextContent(goog.dom.getElement('current_size'),
  53. size.toString());
  54. }
  55. // Initialize the layout.
  56. updateUi(goog.dom.getViewportSize());
  57. // Start listening for viewport size changes.
  58. var vsm = new goog.dom.ViewportSizeMonitor();
  59. goog.events.listen(vsm, goog.events.EventType.RESIZE, function(e) {
  60. updateUi(vsm.getSize());
  61. });
  62. </script>
  63. </body>
  64. </html>