Fixed problems with touch events on a scrollable page

This commit is contained in:
Gregers Gram Rygg
2012-11-02 14:12:29 +01:00
parent 8d0da09454
commit a2a391d3b5
9 changed files with 266 additions and 43 deletions

View File

@@ -903,7 +903,7 @@ OpenLayers.Events = OpenLayers.Class({
var num = touches.length;
var touch;
for (var i=0; i<num; ++i) {
touch = touches[i];
touch = this.getTouchClientXY(touches[i]);
x += touch.clientX;
y += touch.clientY;
}
@@ -915,7 +915,47 @@ OpenLayers.Events = OpenLayers.Class({
}
this.triggerEvent(type, evt);
},
/**
* Method: getTouchClientXY
* WebKit has a few bugs for clientX/clientY. This method detects them
* and calculate the correct values.
*
* Parameters:
* evt - {Event or Touch} Either the event object or a single touch object
*
* Returns:
* {Object} An object with only clientX and clientY properties with the
* calculated values.
*/
getTouchClientXY: function (evt) {
var win = window._mockWin || window,
winPageX = win.pageXOffset,
winPageY = win.pageYOffset,
x = evt.clientX,
y = evt.clientY;
if (evt.pageY === 0 && Math.floor(y) > Math.floor(evt.pageY) ||
evt.pageX === 0 && Math.floor(x) > Math.floor(evt.pageX)) {
// iOS4 include scroll offset in clientX/Y
x = x - winPageX;
y = y - winPageY;
} else if (y < (evt.pageY - winPageY) || x < (evt.pageX - winPageX) ) {
// Some Android browsers have totally bogus values for clientX/Y
// when scrolling/zooming a page
x = evt.pageX - winPageX;
y = evt.pageY - winPageY;
}
evt.olClientX = x;
evt.olClientY = y;
return {
clientX: x,
clientY: y
};
},
/**
* APIMethod: clearMouseCache
* Clear cached data about the mouse position. This should be called any
@@ -925,17 +965,7 @@ OpenLayers.Events = OpenLayers.Class({
clearMouseCache: function() {
this.element.scrolls = null;
this.element.lefttop = null;
// OpenLayers.Util.pagePosition needs to use
// element.getBoundingClientRect to correctly calculate the offsets
// for the iPhone, but once the page is scrolled, getBoundingClientRect
// returns incorrect offsets. So our best bet is to not invalidate the
// offsets once we have them, and hope that the page was not scrolled
// when we did the initial calculation.
var body = document.body;
if (body && !((body.scrollTop != 0 || body.scrollLeft != 0) &&
navigator.userAgent.match(/iPhone/i))) {
this.element.offsets = null;
}
this.element.offsets = null;
},
/**
@@ -959,8 +989,8 @@ OpenLayers.Events = OpenLayers.Class({
if (!this.element.scrolls) {
var viewportElement = OpenLayers.Util.getViewportElement();
this.element.scrolls = [
viewportElement.scrollLeft,
viewportElement.scrollTop
window.pageXOffset || viewportElement.scrollLeft,
window.pageYOffset || viewportElement.scrollTop
];
}