Skip to content

Instantly share code, notes, and snippets.

@leviwheatcroft
Created July 31, 2014 23:58
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 leviwheatcroft/c9607aba440cc1fe0624 to your computer and use it in GitHub Desktop.
Save leviwheatcroft/c9607aba440cc1fe0624 to your computer and use it in GitHub Desktop.
#test {
background-color: #000000;
background-image: url('http://placekitten.com/g/200/300');
display: block;
width: 200px;
height: 1000px;
background-repeat: no-repeat;
margin: 100px 0 0 0;
background-attachment: fixed;
}
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id='test'>
reference reference reference reference reference reference reference reference
</div>
</body>
</html>
(function(jQuery) {
jQuery.fn.parallaxBackground = function(options) {
var defaults = {
offsetX: 0,
offsetY: 0,
ratio: 0.5
};
options = $.extend({}, defaults, options);
var top = this.offset().top;
var target = this;
$(window).scroll(function(event) {
console.log(event.originalEvent.pageY);
console.log(top);
console.log(options.ratio);
//console.log($('#test').offset().top);
var position = '0px ' + ((event.originalEvent.pageY - top) * options.ratio) + 'px';
target.css('background-position', position);
console.log(position);
});
return this;
};
})(jQuery);
$(document).ready(function() {
$('#test').parallaxBackground();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment