Skip to content

Instantly share code, notes, and snippets.

@abass
Last active August 29, 2015 14:01
Show Gist options
  • Save abass/88dda41192eaf38198e4 to your computer and use it in GitHub Desktop.
Save abass/88dda41192eaf38198e4 to your computer and use it in GitHub Desktop.
Parallax Scroll in 5 Lines of jQuery
$(window).on('scroll', function() {
$('.one').parallax(0.5);
$('.two').parallax(0.6);
$('.three').parallax(0.2);
});
.one, .two, .three {
width: 450px;
height: 250px;
}
.one {
background: url('http://lorempixel.com/output/cats-h-c-450-500-6.jpg') no-repeat;
}
.two {
background: url('http://lorempixel.com/output/cats-h-c-450-500-7.jpg') no-repeat;
}
.three {
background: url('http://lorempixel.com/output/cats-h-c-450-500-1.jpg') no-repeat;
}
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
$.fn.parallax = function(strength) {
var scroll_top = $(window).scrollTop();
var move_value = Math.round(scroll_top * strength);
this.css('background-position-y', '-'+ move_value +'px');
};
@abass
Copy link
Author

abass commented May 21, 2014

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