Permalink
Browse files

Fix issue with calculating the element position, this is based on the…

… way jQuery does the same thing
  • Loading branch information...
1 parent 8588fd6 commit 4308f9b9b5ae223501df27bcc0695f7a3d74bdce Ivo Wetzel committed Feb 5, 2013
Showing with 17 additions and 10 deletions.
  1. +17 −10 cocos2d/touch_dispatcher/CCTouchDispatcher.js
@@ -597,18 +597,25 @@ cc.TouchDispatcher.preTouchPoint = cc.p(0, 0);
cc.TouchDispatcher.isRegisterEvent = false;
cc.getHTMLElementPosition = function (element) {
- var pos = null;
- if (element instanceof HTMLCanvasElement) {
- pos = {left:0, top:0, width:element.width, height:element.height};
+ var docElem = document.documentElement;
+ var win = window;
+ var box = null;
+ if (typeof element.getBoundingClientRect === 'function') {
+ box = element.getBoundingClientRect();
+
} else {
- pos = {left:0, top:0, width:parseInt(element.style.width), height:parseInt(element.style.height)};
- }
- while (element != null) {
- pos.left += element.offsetLeft;
- pos.top += element.offsetTop;
- element = element.offsetParent;
+ if (element instanceof HTMLCanvasElement) {
+ box = {left:0, top:0, width:element.width, height:element.height};
+ } else {
+ box = {left:0, top:0, width:parseInt(element.style.width), height:parseInt(element.style.height)};
+ }
}
- return pos;
+ return {
+ left: box.left + win.pageXOffset - docElem.clientLeft,
+ top: box.top + win.pageYOffset - docElem.clientTop,
+ width: box.width,
+ height: box.height
+ };
};
cc.ProcessMouseupEvent = function (element, event) {

0 comments on commit 4308f9b

Please sign in to comment.