Skip to content

Instantly share code, notes, and snippets.

@richardvenneman
Created October 25, 2013 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardvenneman/7153833 to your computer and use it in GitHub Desktop.
Save richardvenneman/7153833 to your computer and use it in GitHub Desktop.
A simple CoffeeScript parallax responsive image implementation
class Parallax
options =
velocity: 15
constructor: ->
@imgs = ($ '.parralax img')
@offset = 0
@prepareImages()
@bindListeners()
prepareImages: ->
for img in @imgs
img = ($ img)
img.offset = img.offset().top
@imgs[_i] = img
scrollParallax: (direction) ->
for img in @imgs
val = (img.offset - window.scrollY) / options.velocity
img.css(y: val)
bindListeners: ->
($ document).on 'scroll', =>
offset = window.scrollY
direction = if offset > @offset then 'down' else 'up'
@scrollParallax direction
@offset = offset
$ ->
parallax = new Parallax()
.illustration {
overflow: hidden;
img {
margin: -6% 0;
width: 100%;
height: auto;
}
}
.illustration.parralax
= image_tag "highlight-open-bg.jpg", { retina: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment