wheelhandler.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2014 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.events.WheelHandler</title>
  10. <link rel="stylesheet" href="../css/checkbox.css">
  11. <style>
  12. #out {
  13. background-color: #eee;
  14. width: 500px;
  15. height: 500px;
  16. position: relative;
  17. display: inline-block;
  18. }
  19. #h-line, #v-line {
  20. position: absolute;
  21. background: black;
  22. }
  23. #h-line {
  24. width: 20px;
  25. height: 1px;
  26. }
  27. #v-line {
  28. width: 1px;
  29. height: 20px;
  30. }
  31. #status {
  32. position: absolute;
  33. bottom: 0;
  34. right: 0;
  35. font: 70% sans-serif;
  36. }
  37. #debug {
  38. font-family: monospace;
  39. white-space: pre;
  40. display: inline-block;
  41. }
  42. </style>
  43. <script src="../base.js"></script>
  44. <script>
  45. goog.require('goog.events');
  46. goog.require('goog.events.WheelHandler');
  47. </script>
  48. </head>
  49. <body>
  50. <h1>goog.events.WheelHandler</h1>
  51. <p>Use your mousewheel on the gray box below to move the cross hair.
  52. <div id=out>
  53. <div id=h-line></div>
  54. <div id=v-line></div>
  55. <div id=status></div>
  56. </div>
  57. <div id=debug>
  58. </div>
  59. <script>
  60. var WheelHandler = goog.events.WheelHandler;
  61. var WHEEL = WheelHandler.EventType.WHEEL;
  62. function $(id) {
  63. return document.getElementById(id)
  64. }
  65. var x = 250, y = 250;
  66. var out = $('out');
  67. var hLine= $('h-line');
  68. var vLine = $('v-line');
  69. var statusLine = $('status');
  70. var debug = $('debug');
  71. var availWidth = out.clientWidth - vLine.offsetWidth;
  72. var availHeight = out.clientHeight - hLine.offsetHeight;
  73. function handleWheel(e) {
  74. x += e.pixelDeltaX;
  75. x = Math.max(0, Math.min(availWidth, x));
  76. y += e.pixelDeltaY;
  77. y = Math.max(0, Math.min(availHeight, y));
  78. updateLines();
  79. e.preventDefault();
  80. var deltaModeString = 'unknown';
  81. var DeltaMode = goog.events.WheelHandler.DeltaMode;
  82. switch (e.deltaMode) {
  83. case DeltaMode.PIXEL:
  84. deltaModeString = 'DeltaMode.PIXEL';
  85. break;
  86. case DeltaMode.LINE:
  87. deltaModeString = 'DeltaMode.LINE';
  88. break;
  89. case DeltaMode.PAGE:
  90. deltaModeString = 'DeltaMode.PAGE';
  91. break;
  92. }
  93. var debugObject = {
  94. type: e.type,
  95. deltaMode: deltaModeString,
  96. deltaX: e.deltaX,
  97. deltaY: e.deltaY,
  98. deltaZ: e.deltaZ,
  99. pixelDeltaX: e.pixelDeltaX,
  100. pixelDeltaY: e.pixelDeltaY,
  101. pixelDeltaZ: e.pixelDeltaZ,
  102. };
  103. debug.innerText = JSON.stringify(debugObject, undefined, 4);
  104. }
  105. function updateLines() {
  106. vLine.style.left = x + 'px';
  107. hLine.style.left = x - hLine.offsetWidth / 2 + 'px';
  108. hLine.style.top = y + 'px';
  109. vLine.style.top = y - vLine.offsetHeight / 2 + 'px';
  110. statusLine.innerText = x + ', ' + y;
  111. }
  112. updateLines();
  113. var mwh = new WheelHandler(out);
  114. goog.events.listen(mwh, WHEEL, handleWheel);
  115. goog.events.listen(window, 'unload', function(e) {
  116. goog.events.unlisten(mwh, WHEEL, handleWheel);
  117. });
  118. </script>
  119. </body>
  120. </html>