2 Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 Use of this source code is governed by a BSD-style license that can be
4 found in the LICENSE file.
8 * @fileoverview Collection of functions which operate on DOM.
11 var domUtils = window['domUtils'] || {};
14 * Returns pageX and pageY of the given element.
16 * @param {Element} element An element of which the top-left position is to be
17 * returned in the coordinate system of the document page.
18 * @return {Object} A point object which has {@code x} and {@code y} fields.
20 domUtils.pageXY = function(element) {
22 for (; element; element = element.offsetParent) {
23 x += element.offsetLeft;
24 y += element.offsetTop;
26 return {'x': x, 'y': y};
30 * Returns pageX and pageY of the given event.
32 * @param {Event} event An event of which the position is to be returned in
33 * the coordinate system of the document page.
34 * @return {Object} A point object which has {@code x} and {@code y} fields.
36 domUtils.pageXYOfEvent = function(event) {
37 return (event.pageX != null && event.pageY != null) ?
38 {'x': event.pageX, 'y': event.pageY} :
39 {'x': event.clientX + document.body.scrollLeft +
40 document.documentElement.scrollLeft,
41 'y': event.clientY + document.body.scrollTop +
42 document.documentElement.scrollTop};