aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2013-04-07 10:24:34 +0000
committerSimon Tatham <anakin@pobox.com>2013-04-07 10:24:34 +0000
commitea25b606cbadaf573b71b132c0d2d5081c8516ab (patch)
tree834779a2a0d639361e2eaf45ada41e65382cf109
parentb341d0544de490f40720401fc523b41088d1666b (diff)
downloadpuzzles-ea25b606cbadaf573b71b132c0d2d5081c8516ab.zip
puzzles-ea25b606cbadaf573b71b132c0d2d5081c8516ab.tar.gz
puzzles-ea25b606cbadaf573b71b132c0d2d5081c8516ab.tar.bz2
puzzles-ea25b606cbadaf573b71b132c0d2d5081c8516ab.tar.xz
Small refactor to relative_mouse_coords: now the functionality which
returns an element's absolute position on the web page is split out into a subfunction that can be called directly. [originally from svn r9819]
-rw-r--r--emccpre.js25
1 files changed, 16 insertions, 9 deletions
diff --git a/emccpre.js b/emccpre.js
index 4ce2209..5c1ad4c 100644
--- a/emccpre.js
+++ b/emccpre.js
@@ -103,21 +103,28 @@ var permalink_seed, permalink_desc;
// The undo and redo buttons. Used by js_enable_undo_redo().
var undo_button, redo_button;
-// Helper function which is passed a mouse event object and a DOM
-// element, and returns the coordinates of the mouse event relative to
-// the top left corner of the element by iterating upwards through the
-// DOM finding each element's offset from its parent, and thus
-// calculating the page-relative position of the target element so
-// that we can subtract that from event.page{X,Y}.
-function relative_mouse_coords(event, element) {
+// Helper function to find the absolute position of a given DOM
+// element on a page, by iterating upwards through the DOM finding
+// each element's offset from its parent, and thus calculating the
+// page-relative position of the target element.
+function element_coords(element) {
var ex = 0, ey = 0;
while (element.offsetParent) {
ex += element.offsetLeft;
ey += element.offsetTop;
element = element.offsetParent;
}
- return {x: event.pageX - ex,
- y: event.pageY - ey};
+ return {x: ex, y:ey};
+}
+
+// Helper function which is passed a mouse event object and a DOM
+// element, and returns the coordinates of the mouse event relative to
+// the top left corner of the element by subtracting element_coords
+// from event.page{X,Y}.
+function relative_mouse_coords(event, element) {
+ var ecoords = element_coords(element);
+ return {x: event.pageX - ecoords.x,
+ y: event.pageY - ecoords.y};
}
// Init function called from body.onload.