Created
November 2, 2012 07:26
-
-
Save fchasen/3999242 to your computer and use it in GitHub Desktop.
hit1
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 Stage(el) { | |
this.el = document.getElementById(el); | |
this.position(); | |
this.listeners(); | |
return this; | |
} | |
Stage.prototype = { | |
position: function(){ | |
var offset = this.offset(this.el); | |
this.positionTop = offset.left - window.scrollX; | |
this.positionLeft = offset.top - window.scrollY; | |
}, | |
offset: function(el){ | |
var _x = 0, _y = 0; | |
while(el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) { | |
_x += el.offsetLeft; | |
_y += el.offsetTop; | |
el = el.offsetParent; | |
} | |
return { top: _y, left: _x }; | |
}, | |
listeners: function(){ | |
var _self = this, | |
output = document.getElementById("output"); | |
window.addEventListener('resize', function(){ | |
_self.position(); | |
}, false); | |
this.el.addEventListener('mousemove', function(e){ | |
var x = e.clientX - _self.positionTop, | |
y = e.clientY - _self.positionLeft; | |
output.innerHTML = (x + "," + y); | |
}, false); | |
} | |
} | |
var stage = new Stage("stage"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment