-
Notifications
You must be signed in to change notification settings - Fork 214
/
parallax.js
172 lines (158 loc) · 4.68 KB
/
parallax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
window.onload = function(){
if (typeof jQuery === 'undefined') {
alert("ERROR: parallax.js requires jQuery.");
} else {
var updateparallax = function(){
parallax.width = window.innerWidth;
parallax.height = window.innerHeight;
}
updateparallax();
$(window).resize(function(){updateparallax();});
}
}
var parallaxPage = function(name, pageHtml){
return{
key: name,
page : pageHtml,
right : function(callback){return this.transition({left:parallax.width,top:0} ,{left:-parallax.width,top:0} ,callback);},
left : function(callback){return this.transition({left:-parallax.width,top:0} ,{left:parallax.width,top:0} ,callback);},
top : function(callback){return this.transition({left:0,top:-parallax.height},{left:0,top:parallax.height} ,callback);},
bottom : function(callback){return this.transition({left:0,top:parallax.height} ,{left:0,top:-parallax.height},callback);},
transition : function(locationNew, locationOld, callback){
if(!parallax.sliding){
parallax.sliding = true;
var thisPage = this;
if(parallax.current !== this){
this.hide(locationNew);
if(typeof parallax.preload === 'function'){
parallax.preload();
}
if(typeof this.preload === 'function'){
this.preload();
}
this.slide({left:0,top:0}, function(){
thisPage.makeCurrent();
parallax.sliding = false;
if(typeof callback === 'function'){
callback();
}
});
if(typeof parallax.current !== 'undefined'){
parallax.current.slide( locationOld,
function(){
parallax.sliding = false;
}
);
}
parallax.slideBackground(locationNew);
}
}
return this;
},
slide : function(css, callback){
this.page.css("display", "block");
this.page.stop().animate(css, parallax.speed, parallax.easing,
function(){if(typeof callback === "function"){callback();}
});
},
hide : function(newLocation){
newLocation = newLocation || {left:parallax.width,top:0}; //defaults left off screen
this.page.css("display", "none");
this.page.css(newLocation);
return this;
},
show : function(newLocation){
newLocation = newLocation || {left:0,top:0}; //defaults on screen
if(typeof parallax.current !== 'undefined'){
parallax.current.hide();
}
this.makeCurrent();
this.page.css("display", "block");
this.page.css(newLocation);
return this;
},
makeCurrent : function(){
if(this === parallax.current){
return false;
}else{
if(typeof parallax.current !== 'undefined'){
parallax.current.hide();
parallax.last = parallax.current;
}
if(parallax.updateUrl === true){ this.updateUrl(); }
if(typeof parallax.onload == 'function'){ parallax.onload();}
if(typeof this.onload === 'function'){ this.onload();}
parallax.current = this;
}
return true;
},
updateUrl : function(){
var url = document.URL;
url = (url.lastIndexOf("#") === -1)? url : url.substring(0, url.lastIndexOf("#"));
window.location.href = url + "#" + this.key;
},
ackbar : function(){ alert(this.key + " thinks it's a trap!"); return this;},
};
};
var parallax = {
speed : 800,
easing : 'swing',
sliding : false,
unusableNames : ["last", "current", "background","onload","updateUrl", "preload"],
scaling : 0.15,
add : function(key,object) {
var check = true;
if(typeof key === 'object'){
try{
object = key
key = key.attr('id');
} catch(err){
check = false;
alert("ERROR:Page object lacks an id");
}
}else if(typeof key !== 'string'){
check = false;
alert("ERROR:undefined key");
}
if(typeof object !== 'object'){
check = false;
alert("ERROR:undefined page");
}
if(check){
validKeyName = true;
for(propName in this){
if(propName === key) {
validKeyName = false;
}
}
if($.inArray(key, this.unusableNames) !== -1){
validKeyName = false;
}
if(validKeyName){
this[key] = parallaxPage(key,object);
this[key].hide();
this[key].page.css("position","absolute");
}else{
alert("ERROR:'"+key+"' cannot be used as a page identifier");
}
}
return this;
},
fromUrl : function(){
var temp = document.URL.lastIndexOf("#")
if(temp !== -1){
pageName = document.URL.substring(temp + 1, document.URL.length);
if(parallax.hasOwnProperty(pageName)){
return parallax[pageName];
}
}
},
slideBackground : function(newLocation){
if(typeof this.background !== 'undefined' && typeof newLocation !== 'undefined'){
$(this.background).animate({
'background-position-x': '+=' + -newLocation.left * parallax.scaling + 'px',
'background-position-y': '+=' + -newLocation.top * parallax.scaling + 'px',
}, parallax.speed, parallax.easing);
}
},
};