Skip to content

Instantly share code, notes, and snippets.

@jaggy
Last active August 29, 2015 13:55
Show Gist options
  • Save jaggy/8775188 to your computer and use it in GitHub Desktop.
Save jaggy/8775188 to your computer and use it in GitHub Desktop.
Delay the element on scroll
$.fn.parallax = function( options ) {
if ( typeof options.opacity === 'undefined' ) options.opacity = false;
if ( typeof options.resistance === 'undefined' ) options.resistance = 1;
// determine the direction of the parallax thingy
if ( typeof options.inverse === 'undefined' ) options.inverse = true;
var element = $( this );
var total = 0;
$( window ).on('scroll', function(){
// calculate the offset
total = window.pageYOffset + window.innerHeight;
// scroll position
var top = $( window ).scrollTop();
// determine the orientation
var sign = ( options.inverse ) ? -1 : 1;
// get the page scroll offset
var offset = ( window.pageYOffset * sign ) / options.resistance;
if( options.opacity ) {
var opacity = ( 100 - top ) / 100;
element.css( { 'opacity' : opacity } );
}
// move the element
element.css( { 'transform' : 'translate(0, ' + offset + 'px)' } );
});
return $( this );
}
@jaggy
Copy link
Author

jaggy commented May 20, 2014

$(selector).parallax({inverse: true, opacity: true; resistance: 2});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment