Last active
August 29, 2015 14:01
-
-
Save abass/88dda41192eaf38198e4 to your computer and use it in GitHub Desktop.
Parallax Scroll in 5 Lines of jQuery
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
$(window).on('scroll', function() { | |
$('.one').parallax(0.5); | |
$('.two').parallax(0.6); | |
$('.three').parallax(0.2); | |
}); |
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
.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; | |
} |
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
<div class="one"></div> | |
<div class="two"></div> | |
<div class="three"></div> |
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(strength) { | |
var scroll_top = $(window).scrollTop(); | |
var move_value = Math.round(scroll_top * strength); | |
this.css('background-position-y', '-'+ move_value +'px'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo - http://jsfiddle.net/Tek57/