Created
January 1, 2015 23:13
-
-
Save juanbrujo/f068670faa89d2c6e9e3 to your computer and use it in GitHub Desktop.
Ridiculously simple background-parallax with CSS/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
<html> | |
<head> | |
<style> | |
.bgParallax { | |
background-image: url('image/background-jpg'); | |
background-position: 50% 0; | |
background-repeat: repeat; | |
background-attachment: fixed; | |
} | |
</style> | |
</head> | |
</html> | |
<body> | |
<div class="bgParallax" data-speed="5"> | |
<article> | |
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reiciendis et sint dolore aut atque, optio in dolores recusandae rerum quia id provident praesentium dolorum doloribus expedita qui velit ad.</p> | |
</article> | |
</div> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script> | |
$('.bgParallax').each(function(){ | |
var $obj = $(this); | |
$(window).scroll(function() { | |
var yPos = -($(window).scrollTop() / $obj.data('speed')); | |
var bgpos = '50% '+ yPos + 'px'; | |
$obj.css('background-position', bgpos ); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment