Skip to content

Commit 9d2a0f3

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 9d2a0f3

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

samples/simple/index.html

+8-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,9 +36,9 @@
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;
@@ -72,7 +76,9 @@
7276
find('capture', 'output', 'enterleave');
7377
events.forEach(function(en) {
7478
capture.addEventListener(en, function(inEvent) {
75-
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']');
79+
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']' +
80+
' pageX/offsetX ' + inEvent.pageX + '/' + inEvent.offsetX +
81+
' pageY/offsetY ' + inEvent.pageY + '/' + inEvent.offsetY);
7682
});
7783
});
7884
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.pageX - 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.pageY - 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)