Skip to content

Instantly share code, notes, and snippets.

@escusado
Last active August 29, 2015 13:57
Show Gist options
  • Save escusado/9426929 to your computer and use it in GitHub Desktop.
Save escusado/9426929 to your computer and use it in GitHub Desktop.
(function(){
$(document).ready(function(){
PoormanParallax = function PoormanParallax(){
//parallax
this.body = body = $('body.breezi');
this.viewPortH = window.innerHeight;
this.bodyH = document.body.clientHeight;
this.scroll = document.body.scrollTop;
this.availableScroll = this.bodyH - this.viewPortH;
this.backgroundScroll = 200;
this.getProportions = function getProportions(scroll, availableSpace){
return (scroll/availableSpace);
};
this.parallaxThatBitch = function parallaxThatBitch(){
this.scroll = document.body.scrollTop;
this.setVertBkndPos(this.body, this.getProportions(this.scroll, this.availableScroll), this.backgroundScroll);
this.parallaxThemStars();
};
this.setVertBkndPos = function setVertBkndPos(element, position, availableSpace){
var pos = (position*availableSpace),
finalPos = (pos.toFixed(0)*-1),
posString = '0% '+finalPos+'px';
element.css('background-position', posString);
};
//planet and stars
this.planetEl = $("[src*='planet']");
this.planetContainer = this.planetEl.parent();
this.topParent = this.planetContainer.parent();
var containerH = this.topParent.height(),
containerW = this.topParent.width(),
celestialBodyTmpl = '<div style="position:absolute; top:0; left:0; height:'+containerH+'px; width:'+containerW+'px; background-repeat:no-repeat;"></div>',
frontDelta = 50,
middleDelta = 20,
backDelta = -100;
frontInt = -4;
middleInit = -4;
backInit = 1.5;
this.starFrontUrl ='http://imgur.com/dj9mvy3.png';
this.startBackUrl = 'http://imgur.com/ibUF0Xp.png';
this.planetUrl = this.planetEl.attr('src');
this.starFrontEl = $(celestialBodyTmpl);
this.planetMiddleEl = $(celestialBodyTmpl);
this.starBackEl = $(celestialBodyTmpl);
this.planetContainer.remove();
this.topParent.append(this.starBackEl);
this.topParent.append(this.planetMiddleEl);
this.topParent.append(this.starFrontEl);
this.topParent.css({
'width' : containerW,
'height' : containerH,
'position' : 'relative',
'overflow' : 'hidden'
});
this.setupStars = function setupStars(){
this.setBackground(this.starFrontEl, this.starFrontUrl);
this.setBackground(this.planetMiddleEl, this.planetUrl);
this.setBackground(this.starBackEl, this.startBackUrl);
this.setVertBkndPos(this.starFrontEl, frontInt, frontDelta);
this.setVertBkndPos(this.planetMiddleEl, middleInit, middleDelta);
this.setVertBkndPos(this.starBackEl, backInit, backDelta);
this.starFrontEl.css('left', '20%');
this.planetMiddleEl.css('left', '40%');
this.starBackEl.css('left', '70%');
};
this.setBackground = function setBackground(el, url){
el.css('background-image', 'url('+url+')');
};
this.parallaxThemStars = function parallaxThemStars(){
this.setVertBkndPos(this.starFrontEl, frontInt-this.getProportions(this.scroll, this.availableScroll), frontDelta);
this.setVertBkndPos(this.planetMiddleEl, middleInit-this.getProportions(this.scroll, this.availableScroll), middleDelta);
this.setVertBkndPos(this.starBackEl, backInit-this.getProportions(this.scroll, this.availableScroll), backDelta);
};
return this;
};
window.poormanParallax = new PoormanParallax();
window.poormanParallax.setupStars();
$(window).scroll(function(){
window.poormanParallax.parallaxThatBitch();
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment