Skip to content

Instantly share code, notes, and snippets.

@lifedraft
Created June 5, 2014 09:22
Show Gist options
  • Save lifedraft/3741b4045f5e78360c46 to your computer and use it in GitHub Desktop.
Save lifedraft/3741b4045f5e78360c46 to your computer and use it in GitHub Desktop.
one page scroll implementation. made to work with offset content, eq. header or footer.
sections = jQuery('.dd_skrollr-numbers-section')
wrapper = jQuery('.dd_skrollr-numbers')
page_bottom = document.body.scrollHeight
# leave top
wrapper_position_top = wrapper.offset().top
wrapper_height = wrapper.height()
wrapper_position_bottom = wrapper_height + wrapper_position_top
window_height = window.innerHeight
footer_height = page_bottom - wrapper_position_bottom
first_height = window_height - wrapper_position_top
last_height = window_height - footer_height
sections.height(window_height)
sections.eq(0).height(first_height)
sections.eq(-1).height(last_height)
wrapper_height_updated = ((sections.length - 2) * window_height) \
+ first_height \
+ last_height
wrapper.height(wrapper_height_updated)
sections.each (index, element) ->
section = jQuery @
css = {}
if index == 0
css.top = 0
else if index == sections.length - 1
css.top = wrapper_height_updated - last_height
else
css.top = first_height + (window_height * (index - 1))
section.css css
body = jQuery('body').addClass('dd_skrollr-active')
animating = false
active = 0
# jup up after reload
setTimeout ->
window.scrollTo(0,0)
, 100
timeout = null
jQuery(document).bind 'mousewheel', ->
return if animating
animating = true
if 0 < event.deltaY
# scroll up
jQuery('html,body').animate
scrollTop: '+=' + window_height
, 1000, "easeOutQuad", ->
clearTimeout timeout
timeout = setTimeout ->
sections.eq(active).trigger('dd_skrollr-rollback')
++active
sections.eq(active).trigger('dd_skrollr-activate')
animating = false
, 200
else
# scroll down
jQuery('html,body').animate
scrollTop: '-=' + window_height
, 1000, "easeOutQuad", ->
clearTimeout timeout
timeout = setTimeout ->
sections.eq(active).trigger('dd_skrollr-rollback')
--active
sections.eq(active).trigger('dd_skrollr-activate')
animating = false
, 200
@section_2()
@section_3()
jQuery(window).load ->
sections.eq(active).trigger('dd_skrollr-activate')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment