Skip to content

Instantly share code, notes, and snippets.

@peyerluk
Created June 23, 2014 08:48
Show Gist options
  • Save peyerluk/09aad7dfe2bf298b1fb2 to your computer and use it in GitHub Desktop.
Save peyerluk/09aad7dfe2bf298b1fb2 to your computer and use it in GitHub Desktop.
Spike for scroll capturing
#= require _swipe_event
settings =
sectionContainer: "section",
easing: "ease",
animationTime: 1000,
pagination: true,
updateURL: false,
keyboard: true,
beforeMove: null,
afterMove: null,
loop: true,
responsiveFallback: false,
direction : 'vertical' # 'vertical' or 'horizontal'
transitionEndEventNames: 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd'
class ld.SlideScroll
constructor: ({ container }) ->
@$container = $(container)
@centering = false
scrollr = new ld.ScrollObserver(elem: @$container[0])
scrollr.enterView =>
@activate()
activate: ->
$(document).on 'mousewheel DOMMouseScroll MozMousePixelScroll', @onScroll
deactivate: ->
$(document).off 'mousewheel DOMMouseScroll MozMousePixelScroll', @onScroll
onScroll: (event) =>
event.preventDefault()
delta = event.originalEvent.wheelDelta || -event.originalEvent.detail
@captureScroll(event, delta)
captureScroll: (event, delta) ->
if delta > 0
@deactivate()
return
return if @centering
# calculate animation speed
if delta < -40
duration = 200
else if delta < -10
duration = 500
else
duration = 1000
# experimental
currentTop = $(window).scrollTop()
targetTop = @$container.offset().top
deltaToTarget = targetTop - currentTop
neededSteps = deltaToTarget / delta
console.log "deltaToTarget #{ deltaToTarget }"
console.log "delta #{ delta }"
# no negative numbers
neededSteps = neededSteps * -1 if neededSteps < 0
console.log "neededSteps #{ neededSteps }"
console.log "duration #{ duration }"
@centering = true
$('html, body').animate
scrollTop: @$container.offset().top
, duration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment