Skip to content

Instantly share code, notes, and snippets.

@hrumhrumble
Last active August 29, 2015 14:07
Show Gist options
  • Save hrumhrumble/b63a1d5251f0882218fb to your computer and use it in GitHub Desktop.
Save hrumhrumble/b63a1d5251f0882218fb to your computer and use it in GitHub Desktop.
# DEMO - http://darenta.ru/
class Car
constructor: ($element)->
@element = $element
@window = $(window)
@car_offset_top = 369
@car_offset_bottom = 123
@window.on "scroll", => @go()
@direction()
start: ->
@element.css
top: @car_offset_top
stop: ->
@element.css
top: @road_height
drive: ->
@element.css
top: @window.scrollTop() * 1.180999181
direction: ->
lastScrollTop = 0
@window.on "scroll", =>
st = @window.scrollTop()
if st > lastScrollTop
console.log "scroll down"
else
console.log "scroll up"
lastScrollTop = st
go: ->
@down = @window.scrollTop() + (@window.height() - (@car_offset_top + @car_offset_bottom))
@road_height = $('#wrap').height() - (@car_offset_bottom + @car_offset_top)
if @window.scrollTop() < @car_offset_top
@start()
else if @down >= @road_height
@stop()
else if @window.scrollTop() > @car_offset_top
@drive()
$ ->
$t = $('#car:not(.done)')
if $t.length > 0
$t.addClass('done')
new Car($t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment