Skip to content

Instantly share code, notes, and snippets.

@mattisfrommars
Last active August 29, 2015 13:58
Show Gist options
  • Save mattisfrommars/10320272 to your computer and use it in GitHub Desktop.
Save mattisfrommars/10320272 to your computer and use it in GitHub Desktop.
Javascript class to perform parallax scrolling animations on arbitrary css properties
function ScalarParallaxAnimation( selector, cssProp, scaleMin, scaleMax, valueMin, valueMax, isInteger ) {
isInteger = isInteger || false;
this.$target = $(selector);
this.cssProp = cssProp;
this.scaleMin = scaleMin;
this.scaleMax = scaleMax;
this.valueMin = valueMin;
this.valueMax = valueMax;
this.isInteger = isInteger;
this.setCssValue( valueMin );
this.attachListener();
}
ScalarParallaxAnimation.prototype = {
setCssValue: function ( value ) {
this.$target.css( this.cssProp, value );
},
// Nice, easy to use wrapper around setScalarCssValue
setScrollValue: function ( scrollValue ) {
this._setScalarCssValue(
scrollValue,
this.cssProp,
this.scaleMin,
this.scaleMax,
this.valueMin,
this.valueMax,
this.isInteger
);
},
_setScalarCssValue: function ( position, cssProperty, scrollMin, scrollMax, valueMin, valueMax, isInteger ) {
var value = this.getScaledValue( position, scrollMin, scrollMax, valueMax, valueMin, isInteger );
this.setCssValue( value );
},
getScaledValue: function ( current, scaleMin, scaleMax, valueMax, valueMin, isInteger ) {
function linearScale ( value, scaleMin, scaleMax, valMin, valMax ){
/**
* (b-a)(x - min)
* f(x) = -------------- + a
* max - min
*/
var scaled = ( ( ( valMax-valMin )*( value - scaleMin ) ) / ( scaleMax - scaleMin ) + valMin );
if ( scaled !== scaled ) { scaled = 0; } // Division by 0
return scaled;
}
function clamp( actual, min, max ) {
var biggest = max > min ? max : min;
var smallest = max > min ? min : max;
actual = Math.min( actual, biggest );
actual = Math.max( actual, smallest );
return actual;
}
function scaleValue ( value, scaleMin, scaleMax, valMin, valMax ) {
return clamp( linearScale( value, scaleMin, scaleMax, valMin, valMax ), valMin, valMax );
}
var value = scaleValue( current, scaleMin, scaleMax, valueMin, valueMax );
if ( isInteger ) {
value = Math.round( value );
}
return value;
},
attachListener: function () {
var $window = $(window);
$window.scroll(function(){
var scrollY = $window.scrollTop();
this.setScrollValue( scrollY );
}.bind(this));
},
detatchListener: function() {
// @TODO: have a function to detach listeners when we are done with the object
}
};
if (!$) { throw new Error("I need jQuery to work"); }
// usage exmaple:
// <div id="divOne">Watch me grow from 60 to 120px high when you scroll between 200 pixels away from the top of the screen</div>
var divOneAnimation = new ScalarParallaxAnimation( '#divOne', 'height', 200, 300, 60, 120, true );
// example 2:
// <div id="divTwo">It works with floats, as well as from high to low values. Watch me fade out</div>
var divTwoAnimation = new ScalarParallaxAnimation( '#divTwo', 'opacity', 200, 300, 1, 0, false );
// for more control, you can also manually set the property
divTwoAnimation.setScrollValue(250);
// which, in this case is the same as
divTwoAnimation.setCssValue(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment