Skip to content

Instantly share code, notes, and snippets.

@cirocosta
Created April 22, 2014 00:28
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 cirocosta/11161328 to your computer and use it in GitHub Desktop.
Save cirocosta/11161328 to your computer and use it in GitHub Desktop.
A simple parallax from the source of together js.
// THE FOLLOWING CODE IS NOT MINE, WILL DO SOMETHING WITH IT LATER. CREDITS TO TOGETHERJS
$(document).ready(function(){
// detect a mobile device
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
// Cache the Window object
$window = $(window);
$('section[data-type="background"]').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 = -($window.scrollTop() / $bgobj.data('speed'));
// Put together our final background position
var coords = '50% '+ yPos + 'px';
if (isMobile.any()) {
//remove parallax
var coords = '';
}
// Move the background
$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