Moved old functions into object
This commit is contained in:
parent
30bd06d867
commit
7f6239e987
1 changed files with 26 additions and 7 deletions
33
parallax.js
33
parallax.js
|
@ -1,20 +1,39 @@
|
||||||
var Parallax = {
|
var Parallax = {
|
||||||
initialise: function() {
|
initialise: function(createEvents) {
|
||||||
|
$(this.ident).each(function(){
|
||||||
|
// Calculate Offset
|
||||||
|
inset = $(this).attr('parallax-inset');
|
||||||
|
image_height = $(this).height();
|
||||||
|
range = image_height / inset;
|
||||||
|
$(this).attr('parallax-offset', range / 2);
|
||||||
|
});
|
||||||
|
console.log("Parallax Initialised.");
|
||||||
|
if (createEvents) {this.createEvents();}
|
||||||
|
this.calculate();
|
||||||
},
|
},
|
||||||
calculate: function() {
|
calculate: function() {
|
||||||
|
window_top = $(window).scrollTop();
|
||||||
|
window_bottom = window_top + $(window).height();
|
||||||
|
$(this.ident).each(function(){
|
||||||
|
if (!is_on_screen(this) || !$(this).attr('parallax-offset') != false) { return; }
|
||||||
|
element = $(this);
|
||||||
|
center_pos = (element.offset().top - $(window).scrollTop()) + (element.height() / 2);
|
||||||
|
perc = -(0.5 - ((window_top - center_pos) / window_bottom));
|
||||||
|
|
||||||
|
pos = Math.round((perc * element.attr('parallax-offset')) * 100) / 100;
|
||||||
|
element.css('background-position',"0px " + pos + "px");
|
||||||
|
});
|
||||||
},
|
},
|
||||||
events: function() {
|
createEvents: function() {
|
||||||
|
$(window).scroll(function() { Parallax.calculate(); });
|
||||||
|
$(window).resize(function() { Parallax.initialise(); })
|
||||||
},
|
},
|
||||||
ident: '.parallax'
|
ident: '.parallax'
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_on_screen(elm) {
|
function is_on_screen(elm) {
|
||||||
|
var vpH = $(window).height(),
|
||||||
var vpH = $(window).height(), // Viewport Height
|
st = $(window).scrollTop(),
|
||||||
st = $(window).scrollTop(), // Scroll Top
|
|
||||||
y = $(elm).offset().top,
|
y = $(elm).offset().top,
|
||||||
elementHeight = $(elm).height();
|
elementHeight = $(elm).height();
|
||||||
return ((y < (vpH + st)) && (y > (st - elementHeight)));
|
return ((y < (vpH + st)) && (y > (st - elementHeight)));
|
||||||
|
|
Reference in a new issue