Skip to content

Commit 74fae8e

Browse files
committed
PointerEvent: Polyfill offsetX/Y as getter
WIP: No idea how to write a unit test for this, so for now it only extends the simple sample. Tested with the sample on Chrome, Firefox and Safari (all OSX) and Android 5 (BrowserStack) and iOS 9.1 (iOS Simulator). Fixes jquery-archive#217
1 parent f4e9355 commit 74fae8e

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

samples/simple/index.html

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
height: 300px;
2222
width: 300px;
2323
text-align: center;
24+
position: relative;
25+
top: 10px;
26+
left: 10px;
27+
margin-bottom: 10px;
2428
}
2529
#enterleave {
2630
background-color: blue;
@@ -32,12 +36,13 @@
3236
left: 25%;
3337
top: 25%;
3438
position: relative;
39+
border: 10px solid gray;
3540
}
3641
#output {
37-
width: 300px;
3842
height: 150px;
3943
overflow: scroll;
4044
white-space: pre;
45+
margin-bottom: 1000em;
4146
}
4247
</style>
4348
<script src="../../dist/pep.js"></script>
@@ -72,7 +77,9 @@
7277
find('capture', 'output', 'enterleave');
7378
events.forEach(function(en) {
7479
capture.addEventListener(en, function(inEvent) {
75-
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']');
80+
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']' +
81+
' pageX/offsetX ' + inEvent.pageX + '/' + inEvent.offsetX +
82+
' pageY/offsetY ' + inEvent.pageY + '/' + inEvent.offsetY);
7683
});
7784
});
7885
enterleave.addEventListener('pointerenter', function(e) {

src/PointerEvent.js

+29
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ function PointerEvent(inType, inDict) {
8787
e.pointerType = inDict.pointerType || '';
8888
e.hwTimestamp = inDict.hwTimestamp || 0;
8989
e.isPrimary = inDict.isPrimary || false;
90+
91+
var rect;
92+
function boundingClientRect() {
93+
if (rect) {
94+
return rect;
95+
}
96+
return (rect = inDict.target.getBoundingClientRect());
97+
}
98+
99+
// CSSOM View Module's extension to mouse events
100+
if (inDict.offsetX === undefined) {
101+
Object.defineProperty(e, 'offsetX', {
102+
get: function() {
103+
return e.clientX - boundingClientRect().left - inDict.target.clientLeft;
104+
}
105+
});
106+
} else {
107+
e.offsetX = inDict.offsetX;
108+
}
109+
if (inDict.offsetY === undefined) {
110+
Object.defineProperty(e, 'offsetY', {
111+
get: function() {
112+
return e.clientY - boundingClientRect().top - inDict.target.clientTop;
113+
}
114+
});
115+
} else {
116+
e.offsetY = inDict.offsetY;
117+
}
118+
90119
return e;
91120
}
92121

0 commit comments

Comments
 (0)