Skip to content

Instantly share code, notes, and snippets.

@maylogger
Created February 8, 2013 10:41
Show Gist options
  • Select an option

  • Save maylogger/4738028 to your computer and use it in GitHub Desktop.

Select an option

Save maylogger/4738028 to your computer and use it in GitHub Desktop.
js 圖片視差滾動機制
///////////////////////////////
// Parallax
///////////////////////////////
// Calcualte the home banner parallax scrolling
function scrollBanner() {
//Get the scoll position of the page
scrollPos = $(this).scrollTop();
//Scroll and fade out the banner text
$('#bannerText').css({
'margin-top' : -(scrollPos/3)+"px",
'opacity' : 1-(scrollPos/300)
});
//Scroll the background of the banner
$('#homeBanner').css({
'background-position' : 'center ' + (-scrollPos/8)+"px"
});
}
///////////////////////////////
// Initialize
///////////////////////////////
$(document).ready(function(){
if(!isMobile()) {
$(window).scroll(function() {
scrollBanner();
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment