Created
August 27, 2012 22:07
-
-
Save jonathantneal/3492777 to your computer and use it in GitHub Desktop.
Exposing more coordinates to the DOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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; | |
} | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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