Created
June 15, 2012 01:44
-
-
Save hugobast/2934175 to your computer and use it in GitHub Desktop.
CoffeeScript Parallax Scrolling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.translate = (x, y) -> | |
translate = "translate(#{x}px, #{y}px)" | |
this.css('-moz-transform', translate); | |
this.css('-o-transform', translate); | |
this.css('-ms-transform', translate); | |
this.css('-webkit-transform', translate); | |
this.css('transform', translate); | |
$.fn.translate3d = (x, y, z) -> | |
translate3d = "translate3d(#{x}px, #{y}px, #{z}px)" | |
this.css('-moz-transform', translate3d); | |
this.css('-o-transform', translate3d); | |
this.css('-ms-transform', translate3d); | |
this.css('-webkit-transform', translate3d); | |
this.css('transform', translate3d); | |
$.fn.parallax = (x, y) -> | |
this.translate x, y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Slide | |
constructor: (slide) -> | |
@el = $(slide.selector) | |
@ratio = slide.ratio | |
@number = slide.number | |
@first = slide.first | |
elements = @el.children('.parallax') | |
@parallaxes = (new Pitch.Slide.Item(el) for el in elements) | |
resize: (width) -> | |
if width < 1024 | |
width = 1024 | |
@el.width(width) | |
@el.height(width * @ratio) | |
@el.find('.background').width(width) | |
@el.find('.background').height(width * @ratio) | |
@update() | |
update: -> | |
# this is fucking bullshit.... | |
if @first and @el.position().top isnt 0 | |
window.compasateOffset = @el.position().top | |
@midpoint = @el.height() / 2 | |
@top = @el.position().top - window.compasateOffset | |
@start = @top - @midpoint | |
@end = @el.height() + @top + @midpoint | |
@start = 0 if @first | |
transition: (position) -> | |
self = @ | |
for parallax in @parallaxes | |
vspeed = parallax.vspeed() | |
hspeed = parallax.hspeed() | |
vcurrent = parallax.vcurrent() | |
hcurrent = parallax.hcurrent() | |
vgoto = vcurrent | |
hgoto = hcurrent | |
if position >= self.end | |
vgoto = self.midpoint * 3 * vspeed | |
hgoto = self.midpoint * 3 * hspeed | |
if position <= self.start | |
if self.first | |
vgoto = 0 | |
hgoto = 0 | |
else | |
vgoto = -self.midpoint * vspeed | |
hgoto = -self.midpoint * hspeed | |
if position < self.end and position > self.start | |
if self.insection(self.position) | |
vgoto += (position - self.position) * vspeed | |
hgoto += (position - self.position) * hspeed | |
else | |
if self.isbelow(position) | |
vgoto += (position - self.end) * vspeed | |
hgoto += (position - self.end) * hspeed | |
else if self.isabove(position) | |
vgoto += (position - self.start) * vspeed | |
hgoto += (position - self.start) * hspeed | |
if position is self.top | |
vgoto = 0 | |
hgoto = 0 | |
parallax.el.data 'vcurrent', vgoto | |
parallax.el.data 'hcurrent', hgoto | |
parallax.move hgoto, vgoto | |
@position = position | |
insection: (position) -> | |
@start <= position and position <= @end | |
isbelow: (position) -> | |
@position > position | |
isabove: (position) -> | |
@position < position | |
class Parallax | |
constructor: (el) -> | |
@el = $(el) | |
move: (x, y) -> | |
@el.parallax x, y | |
vspeed: -> | |
@el.data 'vspeed' | |
hspeed: -> | |
@el.data 'hspeed' | |
hcurrent: -> | |
@el.data 'hcurrent' | |
vcurrent: -> | |
@el.data 'vcurrent' | |
class Slide.Item extends Parallax | |
constructor: (el) -> | |
super el |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment