-
Notifications
You must be signed in to change notification settings - Fork 90
/
parallaxImg.js
executable file
·274 lines (239 loc) · 9.32 KB
/
parallaxImg.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Parallax Images Scroll - by
╱╱╱╱╱╱╱╱╱╱╭╮╭╮╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╭╮
╱╱╱╱╱╱╱╱╱╭╯╰┫┃╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃┃
╭━━┳╮╱╭┳━╋╮╭┫╰━┳┳━━╮╭━━┳━━┳━╮╭━━┫╰━┳━━┳━━━╮
┃╭━┫┃╱┃┃╭╮┫┃┃╭╮┣┫╭╮┃┃━━┫╭╮┃╭╮┫╭━┫╭╮┃┃━╋━━┃┃
┃╰━┫╰━╯┃┃┃┃╰┫┃┃┃┃╭╮┃┣━━┃╭╮┃┃┃┃╰━┫┃┃┃┃━┫┃━━┫
╰━━┻━╮╭┻╯╰┻━┻╯╰┻┻╯╰╯╰━━┻╯╰┻╯╰┻━━┻╯╰┻━━┻━━━╯
╱╱╱╭━╯┃/////////////////////////////////////
╱╱╱╰━━╯http://cynt.co.nf////////////////////
http://cyntss.github.io/Parallax-img-scroll/
////////////////////////////////////////////
V.1.2.5 - MIT license. Allowed for commercial
and personal use =D
*/
//reset the scroll to 0 (top of page)
$(window).on('beforeunload', function() {
$(window).scrollTop(0);
});
function parallaxImgScroll(settings) {
//if the user is setting the configuration
var default_settings = {
initialOpacity : 0, //from 0 to 1, e.g. 0.34 is a valid value. 0 = transparent, 1 = Opaque
opacitySpeed : 0.02, //values from 0.01 to 1 -> 0.01: slowly appears on screen; 1: appears as soon as the user scrolls 1px
pageLoader: false // boolean type
}
var parallaxSettings = $.extend({}, default_settings, settings);
//definition of essential variables [do not modify]
var parallaxElementsArray = [];
var lastestScrolled = 0;
var scrolled = 0;
$(document).ready(function (){
$(".parallax-move").css({
'opacity' : 0,
position: "absolute"
});
if (parallaxSettings.pageLoader) {
var loadingMaringTop = $(window).height() / 2
$("body").wrapInner( "<div class='parallaxImg-page'></div>");
$("body").css({
height: '100%',
width: '100%'
})
$("body").prepend("<div class='parallaxImg-loading-page'></div>")
$(".parallaxImg-loading-page").css({
position: 'absolute',
top: '0px',
left: '0px',
width: '100%',
height: '100%',
background: '#333',
})
$(".parallaxImg-loading-page").prepend("<div class='parallaxImg-loading-text'>Loading Page</div>")
$(".parallaxImg-loading-text").css({
width: '300px',
'margin-left': 'auto',
'margin-right': 'auto',
'text-align': 'center',
'padding-top': loadingMaringTop + 'px'
})
$(".parallaxImg-page").hide()
}
})
$(window).load(function() {
$(".parallax-move").css({
'opacity' : parallaxSettings.initialOpacity
});
if (parallaxSettings.pageLoader) {
$(".parallaxImg-loading-page").fadeOut('600', function() {
$(".parallaxImg-page").fadeIn()
$(this).remove()
parallaxImgInit();
})
}
else {
parallaxImgInit();
}
/* Scroll event to trigger the function */
$(window).bind('scroll',function(e){
parallaxImgScroll();
});
});
/* Initial setup of the elements */
function parallaxImgInit() {
$(".parallax-img-container").each(function() {
var widthOfContainer = $(this).width();
var heightOfContainer = $(this).height();
var setOfElements = $(this).children();
for (i = 0; i < setOfElements.length ; i++) {
var classApplied = $(setOfElements[i]).attr('class');
if (classApplied != "parallax-move") {
$(setOfElements[i]).css({
"z-index": 100,
"position": "relative"
})
}
// for all the elements that have the class "parallax-move"
else {
// if the element has a Speed declared
if ($(setOfElements[i]).hasData('ps-speed')) {
scrollSpeed = $(setOfElements[i]).data('ps-speed');
}
else {
var ranNumSpeed = Math.floor((Math.random() * 100) + 1);
if(ranNumSpeed < 10) {
var scrollSpeed = "0.0" + ranNumSpeed;
}
else {
var scrollSpeed = "0." + ranNumSpeed;
}
}
//if the element has a vertical position declared
if ($(setOfElements[i]).hasData('ps-vertical-position')) {
TopPosition = $(setOfElements[i]).data('ps-vertical-position');
}
else {
var TopPosition = Math.floor(Math.random() * (heightOfContainer - (heightOfContainer/4)) + 1);
}
//if the element has an horizontal position declared
if ($(setOfElements[i]).hasData('ps-horizontal-position')) {
var leftPosition = $(setOfElements[i]).data('ps-horizontal-position');
var rightPosition = undefined;
}
else if ($(setOfElements[i]).hasData('ps-horizontal-position-right')) {
var rightPosition = $(setOfElements[i]).data('ps-horizontal-position-right');
var leftPosition = undefined;
} else {
var leftPosition = Math.floor(Math.random() * (widthOfContainer - 100) + 50);
var rightPosition = undefined;
}
//if the element has a z-index declared
if ($(setOfElements[i]).hasData('ps-z-index')) {
var zPosition = $(setOfElements[i]).data('ps-z-index');
}
else {
var zPosition = Math.floor(Math.random() * 10 + 1);
}
parallaxElementsArray.push({
"element" : $(setOfElements[i]),
"scrollSpeed" : scrollSpeed,
"horizontalPagePosition" : leftPosition,
"horizontalPagePositionRight" : rightPosition,
"verticalPagePosition" : TopPosition,
"opacity" : parallaxSettings.initialOpacity,
"privateScrolled" : 0
});
/* Apply initial position */
console.log(leftPosition, rightPosition)
if (leftPosition) {
$(setOfElements[i]).css({
"bottom": TopPosition,
"left": leftPosition,
"z-index": zPosition
})
} else {
$(setOfElements[i]).css({
"bottom": TopPosition,
"right": rightPosition,
"z-index": zPosition
})
}
}
}
});
$(".parallax-img-container").css({
position: "relative",
overflow: "hidden"
});
}
/* Move the images while scrolling the page */
function parallaxImgScroll() {
scrolled = $(window).scrollTop();
for (i = 0; i < parallaxElementsArray.length; i++) {
alpha = parallaxElementsArray[i].opacity;
/* Calculate the distance between the element and the top of the document */
var distanceFromTop = $(parallaxElementsArray[i].element).offset().top;
var elementHeight = $(parallaxElementsArray[i].element).height();
if (isVisible(distanceFromTop, elementHeight)) {
/* if scrolling down */
if (lastestScrolled < scrolled) {
/* unless parallaxSettings.opacitySpeed = 1, make the element appear progressively */
if (parallaxSettings.initialOpacity != 1) {
alpha = alpha + parallaxSettings.opacitySpeed;
if (alpha > 1) {
alpha = 1;
}
} else {
alpha = parallaxSettings.initialOpacity;
}
//save the scrolling for this element
parallaxElementsArray[i].privateScrolled = parallaxElementsArray[i].privateScrolled + (scrolled - lastestScrolled);
}
else if (scrolled == 0) {
alpha = parallaxSettings.initialOpacity;
parallaxElementsArray[i].privateScrolled = 0;
/* else.. if scrolling up */
}
else {
/* unless parallaxSettings.opacitySpeed = 1, make the element appear progressively */
if (parallaxSettings.initialOpacity != 1) {
alpha = alpha - parallaxSettings.opacitySpeed;
if (alpha < parallaxSettings.initialOpacity) {
alpha = parallaxSettings.initialOpacity;
}
} else {
alpha = parallaxSettings.initialOpacity;
}
//save the scrolling for this element
parallaxElementsArray[i].privateScrolled = parallaxElementsArray[i].privateScrolled - (lastestScrolled - scrolled);
}
$(parallaxElementsArray[i].element).css({
"opacity" : alpha,
'bottom': (parallaxElementsArray[i].verticalPagePosition + (parallaxElementsArray[i].privateScrolled * parallaxElementsArray[i].scrollSpeed)) + 'px'
});
/* save the opacity in the elements object */
parallaxElementsArray[i].opacity = alpha;
}
}
lastestScrolled = scrolled;
/* check if the element is visible on screen */
function isVisible(distance, height) {
//if it went up and off the screen
if ([distance + height] < scrolled) {
return false;
}
//if if didnt appear from belog yet
else if ([scrolled + $(window).height()] < distance) {
return false;
}
//if it is being displayed on screen
else {
return true;
}
}
}
}
/* check if a data attribute exists */
$.fn.hasData = function(attrName) {
return (typeof $(this).data(attrName) != 'undefined');
};