Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created August 27, 2012 22:07
Show Gist options
  • Save jonathantneal/3492777 to your computer and use it in GitHub Desktop.
Save jonathantneal/3492777 to your computer and use it in GitHub Desktop.
Exposing more coordinates to the DOM
(function () {
function define(object, properties) {
for (var property in properties) !(property in object) && Object.defineProperty(object, property, { get: properties[property], enumerable: true });
}
define(Element.prototype, {
width: function () {
return this.getBoundingClientRect().width;
},
height: function () {
return this.getBoundingClientRect().height;
},
clientX: function () {
return this.getBoundingClientRect().left;
},
clientY: function () {
return this.getBoundingClientRect().top;
},
pageX: function () {
return this.getBoundingClientRect().left + window.pageXOffset;
},
pageY: function () {
return this.getBoundingClientRect().top + window.pageYOffset;
}
});
})();
(function (window, document) {
function define(object, properties) {
for (var property in properties) !(property in object) && Object.defineProperty(object, property, { get: properties[property] });
}
define(window, {
pageXOffset: function () {
return (document.documentElement.scrollLeft || document.body.scrollLeft || 0) - (document.documentElement.clientLeft || document.body.clientLeft || 0);
},
pageYOffset: function () {
return (document.documentElement.scrollTop || document.body.scrollTop || 0) - (document.documentElement.clientTop || document.body.clientTop || 0);
}
});
define(Element.prototype, {
width: function (coordinates) {
return (coordinates = this.getBoundingClientRect()), coordinates.right - coordinates.left;
},
height: function (coordinates) {
return (coordinates = this.getBoundingClientRect()), coordinates.bottom - coordinates.top;
},
clientX: function () {
return this.getBoundingClientRect().left;
},
clientY: function () {
return this.getBoundingClientRect().top;
},
pageX: function () {
return this.getBoundingClientRect().left + window.pageXOffset;
},
pageY: function () {
return this.getBoundingClientRect().top + window.pageYOffset;
}
});
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment