Skip to content

Instantly share code, notes, and snippets.

@mapsam
Created September 19, 2014 17:27
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 mapsam/971f4ca90bf7c230f6f7 to your computer and use it in GitHub Desktop.
Save mapsam/971f4ca90bf7c230f6f7 to your computer and use it in GitHub Desktop.
simple parallax
#parallax-wrap {
height: 400px; /* anything you want/need */
width: 100%;
position: relative;
display: block;
}
.parallax {
position:absolute;
width:100%;
height:100%;
background-image: url(images/parallax.jpg);
background-repeat: no-repeat;
background-position:center center;
background-attachment:fixed;
background-size: cover;
}
<div id="parallax-wrap">
<div class="parallax" data-speed="5"></div>
</div>
function parallaxBG(item,speed) {
var pos = item.offset();
$(window).scroll(function() {
var yPos = -(($(window).scrollTop()-pos.top)/speed);
var coords = 'center '+ yPos + 'px';
item.css({ 'background-position': coords });
});
}
function initParallax() {
$('.parallax').each(function() {
var speed = ($(this).attr('data-speed')) ? parseInt($(this).attr('data-speed')): 10;
parallaxBG($(this),speed);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment