mousewheelhandler.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.events.MouseWheelHandler</title>
  10. <link rel="stylesheet" href="../css/checkbox.css">
  11. <style>
  12. #out {
  13. background-color: #eee;
  14. width: 200px;
  15. height: 200px;
  16. position: relative;
  17. }
  18. #h-line, #v-line {
  19. position: absolute;
  20. background: black;
  21. }
  22. #h-line {
  23. width: 20px;
  24. height: 1px;
  25. }
  26. #v-line {
  27. width: 1px;
  28. height: 20px;
  29. }
  30. #status {
  31. position: absolute;
  32. bottom: 0;
  33. right: 0;
  34. font: 70% sans-serif;
  35. }
  36. </style>
  37. <script src="../base.js"></script>
  38. <script>
  39. goog.require('goog.events');
  40. goog.require('goog.events.MouseWheelHandler');
  41. </script>
  42. </head>
  43. <body>
  44. <h1>goog.events.MouseWheelHandler</h1>
  45. <p>Use your mousewheel on the gray box below to move the cross hair.
  46. <div id=out>
  47. <div id=h-line></div>
  48. <div id=v-line></div>
  49. <div id=status></div>
  50. </div>
  51. <script>
  52. var MouseWheelHandler = goog.events.MouseWheelHandler;
  53. var MOUSEWHEEL = MouseWheelHandler.EventType.MOUSEWHEEL;
  54. function $(id) {
  55. return document.getElementById(id)
  56. }
  57. var x = 100, y = 100;
  58. var out = $('out');
  59. var hLine= $('h-line');
  60. var vLine = $('v-line');
  61. var status = $('status');
  62. var availWidth = out.clientWidth - vLine.offsetWidth;
  63. var availHeight = out.clientHeight - hLine.offsetHeight;
  64. function handleMouseWheel(e) {
  65. x += e.deltaX / 3;
  66. x = Math.max(0, Math.min(availWidth, x));
  67. y += e.deltaY / 3;
  68. y = Math.max(0, Math.min(availHeight, y));
  69. updateLines();
  70. e.preventDefault();
  71. }
  72. function updateLines() {
  73. vLine.style.left = x + 'px';
  74. hLine.style.left = x - hLine.offsetWidth / 2 + 'px';
  75. hLine.style.top = y + 'px';
  76. vLine.style.top = y - vLine.offsetHeight / 2 + 'px';
  77. status.innerHTML = x + ', ' + y;
  78. }
  79. updateLines();
  80. var mwh = new MouseWheelHandler(out);
  81. goog.events.listen(mwh, MOUSEWHEEL, handleMouseWheel);
  82. goog.events.listen(window, 'unload', function(e) {
  83. goog.events.unlisten(mwh, MOUSEWHEEL, handleMouseWheel);
  84. });
  85. </script>
  86. </body>
  87. </html>