Last active
August 29, 2015 13:55
-
-
Save jaggy/8775188 to your computer and use it in GitHub Desktop.
Delay the element on scroll
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.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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$(selector).parallax({inverse: true, opacity: true; resistance: 2});