Skip to content

Instantly share code, notes, and snippets.

@bobuss
Created June 21, 2012 11:05
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 bobuss/2965142 to your computer and use it in GitHub Desktop.
Save bobuss/2965142 to your computer and use it in GitHub Desktop.
Horizontal scroll in the background

Scrollable.js

Just add a "scrollable" class name to your background element, then it will scroll from left to right, according to your scroll event.

$(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