Skip to content

Instantly share code, notes, and snippets.

@jobayerarman
Last active November 19, 2015 23:24
Show Gist options
  • Select an option

  • Save jobayerarman/a54bf8d45e5b75de43a5 to your computer and use it in GitHub Desktop.

Select an option

Save jobayerarman/a54bf8d45e5b75de43a5 to your computer and use it in GitHub Desktop.
/* Parallax Scrolling */
$(function parallaxScrolling() {
$window = $(window); // Cache the Window object
var width = $window.width(); // Cache the window width
var $bgobj = $('.parallax'); // assigning the object
$('.parallax').each(function(){
var $bgobj = $(this); // assigning the object
$(window).scroll(function() {
// Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = Math.round(-($window.scrollTop() / $bgobj.data('speed')));
// Put together our final background position
var coords = '50% '+ yPos + 'px';
// Move the background only on desktop
if(width > 768) {
$bgobj.css({ backgroundPosition: coords });
}
}); // window scroll Ends
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment