Just add a "scrollable" class name to your background element, then it will scroll from left to right, according to your scroll event.
Created
June 21, 2012 11:05
-
-
Save bobuss/2965142 to your computer and use it in GitHub Desktop.
Horizontal scroll in the background
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() { | |
var element, scrollValue, documentHeight, percent; | |
$('.scrollable').each(function() { | |
element = this; | |
$(element).css('position', 'fixed') | |
.css('top', 0) | |
.css('left', 0) | |
.css('width', '200%') | |
.css('height', '100%') | |
.css('z-index', 0); | |
function scrollBackground() { | |
documentHeight = $(document).height(); | |
scrollValue = $(document).scrollTop(); | |
var percent = Math.floor(scrollValue / (documentHeight) * 100); | |
$(element).css('margin-left', '-' + percent + '%'); | |
} | |
scrollBackground(); | |
$(window).scroll(function(e) { | |
scrollBackground(); | |
}); | |
$(window).resize(function(e) { | |
scrollBackground(); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment