Skip to content

Instantly share code, notes, and snippets.

@andrezrv
Last active December 28, 2015 16:39
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 andrezrv/7530179 to your computer and use it in GitHub Desktop.
Save andrezrv/7530179 to your computer and use it in GitHub Desktop.
How to slow the scrolling of an HTML element to create a simple parallax effect using jQuery.
jQuery( document ).ready( function( $ ) {
var scrollable = $('#branding .navbar-inner');
var difference = 5;
if ( scrollable.length ) {
var a = document.body;
var e = document.documentElement;
$( window ).unbind( 'scroll' ).scroll( function () {
scrollable.css( 'background-position', '0px ' + -( Math.max( e.scrollTop, a.scrollTop ) / difference ) + 'px' );
} );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment