Last active
October 6, 2015 08:28
-
-
Save knorthfield/2965940 to your computer and use it in GitHub Desktop.
Throttled jQuery Parallax
This file contains hidden or 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Parallax</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> | |
<script> | |
$(function(){ | |
var $scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $('body'), | |
$scrollItems = $('[data-scroll-speed]').css('position','relative'), | |
scrolled = false; | |
$(window).scroll(function() { | |
scrolled = true; | |
}); | |
setInterval(function() { | |
if ( scrolled ) { | |
scrolled = false; | |
$scrollItems.each(function() { | |
var $this = $(this), | |
offset = parseInt( parseFloat($this.data('scroll-speed')) * $scroll.scrollTop() ) * -1; | |
$this.css('top', offset); | |
}); | |
} | |
}, 40); | |
}); | |
</script> | |
<style> | |
body { | |
height: 2000px; | |
} | |
div { | |
background: red; | |
width: 200px; | |
height: 200px; | |
} | |
</style> | |
</head> | |
<body> | |
<div></div> | |
<div style="margin-left: 300px;" data-scroll-speed="1.05"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment