Skip to content

Commit 3877a21

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 f9a7ef9 commit 3877a21

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

samples/simple/index.html

+7-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;
@@ -34,7 +38,6 @@
3438
position: relative;
3539
}
3640
#output {
37-
width: 300px;
3841
height: 150px;
3942
overflow: scroll;
4043
white-space: pre;
@@ -72,7 +75,9 @@
7275
find('capture', 'output', 'enterleave');
7376
events.forEach(function(en) {
7477
capture.addEventListener(en, function(inEvent) {
75-
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']');
78+
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']' +
79+
' pageX/offsetX ' + inEvent.pageX + '/' + inEvent.offsetX +
80+
' pageY/offsetY ' + inEvent.pageY + '/' + inEvent.offsetY);
7681
});
7782
});
7883
enterleave.addEventListener('pointerenter', function(e) {

src/PointerEvent.js

+21
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ function PointerEvent(inType, inDict) {
8787
e.pointerType = inDict.pointerType || '';
8888
e.hwTimestamp = inDict.hwTimestamp || 0;
8989
e.isPrimary = inDict.isPrimary || false;
90+
91+
// CSSOM View Module's extension to mouse events
92+
if (inDict.offsetX === undefined) {
93+
Object.defineProperty(e, 'offsetX', {
94+
get: function() {
95+
return e.pageX - inDict.target.getBoundingClientRect().left;
96+
}
97+
});
98+
} else {
99+
e.offsetX = inDict.offsetX;
100+
}
101+
if (inDict.offsetY === undefined) {
102+
Object.defineProperty(e, 'offsetY', {
103+
get: function() {
104+
return e.pageY - inDict.target.getBoundingClientRect().top;
105+
}
106+
});
107+
} else {
108+
e.offsetY = inDict.offsetY;
109+
}
110+
90111
return e;
91112
}
92113

0 commit comments

Comments
 (0)