Created
March 8, 2015 18:17
-
-
Save dabbott/458935dd864c9ea84f7e to your computer and use it in GitHub Desktop.
nearestPage method
This file contains hidden or 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
# Scroll to the nearest page | |
nearestPage = (layerCount, pageSize, value, velocity) -> | |
# Scroll position is negative... easier to think in terms of positive values | |
value = - value | |
lowerPageIndex = Math.max 0, Math.floor(value / pageSize) | |
upperPageIndex = Math.min (layerCount - 1), (lowerPageIndex + 1) | |
if velocity < -0.2 | |
return - upperPageIndex * pageSize | |
else if velocity > 0.2 | |
return - lowerPageIndex * pageSize | |
distanceToLowerPage = Math.abs(value - lowerPageIndex * pageSize) | |
distanceToUpperPage = Math.abs(value - upperPageIndex * pageSize) | |
if distanceToLowerPage < distanceToUpperPage | |
return - lowerPageIndex * pageSize | |
else | |
return - upperPageIndex * pageSize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment