Skip to content

Instantly share code, notes, and snippets.

@dabbott
Created March 8, 2015 18:17
Show Gist options
  • Save dabbott/458935dd864c9ea84f7e to your computer and use it in GitHub Desktop.
Save dabbott/458935dd864c9ea84f7e to your computer and use it in GitHub Desktop.
nearestPage method
# 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