Created
September 19, 2014 17:27
-
-
Save mapsam/971f4ca90bf7c230f6f7 to your computer and use it in GitHub Desktop.
simple parallax
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
#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; | |
} |
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 id="parallax-wrap"> | |
<div class="parallax" data-speed="5"></div> | |
</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
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