| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 5 |
|
| 6 |
Use of this source code is governed by the Apache License, Version 2.0. |
| 7 |
See the COPYING file for details. |
| 8 |
--> |
| 9 |
<head> |
| 10 |
<title>goog.events.MouseWheelHandler</title> |
| 11 |
<link rel="stylesheet" href="../css/checkbox.css"> |
| 12 |
<style> |
| 13 |
#out { |
| 14 |
background-color: #eee; |
| 15 |
width: 200px; |
| 16 |
height: 200px; |
| 17 |
position: relative; |
| 18 |
} |
| 19 |
|
| 20 |
#h-line, #v-line { |
| 21 |
position: absolute; |
| 22 |
background: black; |
| 23 |
} |
| 24 |
|
| 25 |
#h-line { |
| 26 |
width: 20px; |
| 27 |
height: 1px; |
| 28 |
} |
| 29 |
|
| 30 |
#v-line { |
| 31 |
width: 1px; |
| 32 |
height: 20px; |
| 33 |
} |
| 34 |
|
| 35 |
#status { |
| 36 |
position: absolute; |
| 37 |
bottom: 0; |
| 38 |
right: 0; |
| 39 |
font: 70% sans-serif; |
| 40 |
} |
| 41 |
|
| 42 |
</style> |
| 43 |
<script src="../base.js"></script> |
| 44 |
<script> |
| 45 |
|
| 46 |
goog.require('goog.events'); |
| 47 |
goog.require('goog.events.MouseWheelHandler'); |
| 48 |
|
| 49 |
</script> |
| 50 |
</head> |
| 51 |
<body> |
| 52 |
|
| 53 |
<h1>goog.events.MouseWheelHandler</h1> |
| 54 |
|
| 55 |
<p>Use your mousewheel on the gray box below to move the cross hair. |
| 56 |
|
| 57 |
<div id=out> |
| 58 |
<div id=h-line></div> |
| 59 |
<div id=v-line></div> |
| 60 |
<div id=status></div> |
| 61 |
</div> |
| 62 |
|
| 63 |
<script> |
| 64 |
|
| 65 |
var MouseWheelHandler = goog.events.MouseWheelHandler; |
| 66 |
var MOUSEWHEEL = MouseWheelHandler.EventType.MOUSEWHEEL; |
| 67 |
|
| 68 |
function $(id) { |
| 69 |
return document.getElementById(id) |
| 70 |
} |
| 71 |
|
| 72 |
var x = 100, y = 100; |
| 73 |
var out = $('out'); |
| 74 |
var hLine= $('h-line'); |
| 75 |
var vLine = $('v-line'); |
| 76 |
var status = $('status'); |
| 77 |
|
| 78 |
var availWidth = out.clientWidth - vLine.offsetWidth; |
| 79 |
var availHeight = out.clientHeight - hLine.offsetHeight; |
| 80 |
|
| 81 |
function handleMouseWheel(e) { |
| 82 |
x += e.deltaX / 3; |
| 83 |
x = Math.max(0, Math.min(availWidth, x)); |
| 84 |
y += e.deltaY / 3; |
| 85 |
y = Math.max(0, Math.min(availHeight, y)); |
| 86 |
updateLines(); |
| 87 |
e.preventDefault(); |
| 88 |
} |
| 89 |
|
| 90 |
function updateLines() { |
| 91 |
vLine.style.left = x + 'px'; |
| 92 |
hLine.style.left = x - hLine.offsetWidth / 2 + 'px'; |
| 93 |
hLine.style.top = y + 'px'; |
| 94 |
vLine.style.top = y - vLine.offsetHeight / 2 + 'px'; |
| 95 |
status.innerHTML = x + ', ' + y; |
| 96 |
} |
| 97 |
|
| 98 |
updateLines(); |
| 99 |
|
| 100 |
var mwh = new MouseWheelHandler(out); |
| 101 |
goog.events.listen(mwh, MOUSEWHEEL, handleMouseWheel); |
| 102 |
|
| 103 |
goog.events.listen(window, 'unload', function(e) { |
| 104 |
goog.events.unlisten(mwh, MOUSEWHEEL, handleMouseWheel); |
| 105 |
}); |
| 106 |
|
| 107 |
</script> |
| 108 |
</body> |
| 109 |
</html> |