Created
September 10, 2010 22:36
-
-
Save filmaj/574501 to your computer and use it in GitHub Desktop.
Poor man's JavaScript scrolling helper for mobile devices
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 PoorMansGloveBox(element) { | |
this.element = element; | |
this.parentHeight = this.element.parentNode.clientHeight; | |
this.element.style.position = 'relative'; | |
this.element.style.top = '0px'; | |
this.lastPos = 0; | |
this.currentPos = 0; | |
var self = this; | |
this.element.addEventListener('touchstart', function(e){ | |
self.lastPos = e.touches[0].pageY; | |
}, false); | |
this.element.addEventListener('touchmove', function(e){ | |
self.currentPos = e.touches[0].pageY; | |
var deltaY = self.currentPos - self.lastPos; | |
var newPos = (parseInt(self.element.style.top.substr(0,self.element.style.top.length-2)) + deltaY); | |
if (newPos > 0) newPos = 0; | |
var viewportDelta = (self.element.clientHeight - self.parentHeight)*1.2; | |
var maxScrollBottom = viewportDelta<=0?viewportDelta:viewportDelta*-1; | |
if (newPos < maxScrollBottom) newPos = maxScrollBottom; | |
self.element.style.top = newPos + 'px'; | |
self.lastPos += deltaY; | |
}, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
waiting to see the Poorest!