This repository has been archived by the owner on Mar 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fade-on-scroll.js
188 lines (152 loc) · 5.47 KB
/
fade-on-scroll.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
/*!
* Synchronize opacity with scroll position.
* @author WebPraktikos <[email protected]> @webpraktikos
* @version 1.0.1
* @license MIT License
*/
;(function(window) {
'use strict';
/**
* Outer function that holds shared "page container" for all instances.
* @param {string} _page - Element on which the scroll listener is attached.
* @return {function} - Inner function.
*/
window.FadeOnScroll = function(_page) {
var page = _page || 'window';
/**
* Change opacity of the element.
* @param {array} _elementsArr - Elements and its options.
*/
var doInstance = function(_elementsArr) {
var _arguments = arguments;
/**
* Calculates opacity for the specified element based on its position.
* @param {string} _element - Element which will change its opacity.
* @param {object} _options - Custom options for the element.
* @return {array} - Element and its opacity.
*/
var getElOpct = function(_element, _options) {
var contex, toEnd;
var element = document.querySelectorAll(_element);
/** Merges default options with the passed options. */
var options = extend({}, defaultOptions);
if (_options) {
extend(options, _options);
}
/** Default options. */
var defaultOptions = {
direction: 'bottom',
waypoint: 'self'
}
var inrVpHeight = window.innerHeight,
elRect = element[0].getBoundingClientRect(),
elHeight = elRect.height,
elTopFromVPTop = elRect.top,
elTopFromVPBtm = inrVpHeight - elTopFromVPTop,
elBtmFromVPTop = elTopFromVPTop + elHeight;
if (options.waypoint.indexOf('%') > -1) {
var inrVpHeightFraction = ( 100 / parseInt(options.waypoint, 10) );
contex = ( inrVpHeight / inrVpHeightFraction.toFixed(2) );
} else if (options.waypoint.indexOf('px') > -1) {
contex = parseInt(options.waypoint, 10);
} else if (options.waypoint === 'self') {
contex = elHeight;
}
if (options.direction === 'top') {
toEnd = elBtmFromVPTop;
}
if (options.direction === 'bottom') {
toEnd = elTopFromVPBtm;
}
if (options.direction === 'both') {
var toTop = (elBtmFromVPTop / contex);
toEnd = ( (toTop <= 1) ? elBtmFromVPTop : elTopFromVPBtm );
}
var opct = (toEnd / contex).toFixed(2);
return [_element, opct];
}
/**
* Gets opacity for all elements.
* @return {array} - Array of arrays made of element and its opacity.
*/
var getElOpctArr = function() {
if (Object.prototype.toString.call(_arguments[0] ) === '[object Array]') {
return _elementsArr.map(function(item) {
return getElOpct(item[0], item[1]);
});
} else {
return [getElOpct(_arguments[0], _arguments[1])];
}
}
/** Changes opacity on the specified element or elements. */
var changeOpct = function() {
getElOpctArr().forEach(function(item) {
Array.prototype.slice.call( document.querySelectorAll(item[0]) )
.forEach(function(subItem) {
subItem.style.opacity = item[1]
})
})
}
/** Changes opacity without waiting for scroll. */
changeOpct();
var onScroll = function() {
var container = (page === 'window') ? window : document.querySelector(page);
container.addEventListener('scroll', function() {
window.requestAnimationFrame(changeOpct);
})
}
onScroll();
}
/**
* Search for elements with the class name "fade-on-scroll"
* and apply "doInstance" function on each.
*/
var initFromHtml = function() {
var elmsFromHtmlArr = Array.prototype.slice.call( document.querySelectorAll( '.fade-on-scroll' ) )
.reduce(function( previous , value , index ) {
previous.push( [ '.' + Array.prototype.slice.call( value.classList ).join( '.' ) + '[data-fos-options]',
JSON.parse( value.getAttribute( 'data-fos-options' ) ) ] );
return previous;
}, [] );
doInstance( elmsFromHtmlArr );
}();
return doInstance;
}
/**
* Utilities and polyfills.
****/
/** Utility function for object merging. */
function extend(a, b) {
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
/**
* Polyfill for "requestAnimationFrame".
*/
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for( var x = 0 ; x < vendors.length && ! window.requestAnimationFrame ; ++x ) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if ( ! window.requestAnimationFrame )
window.requestAnimationFrame = function( callback , element ) {
var currTime = new Date().getTime();
var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ));
var id = window.setTimeout(function() { callback( currTime + timeToCall ); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if ( ! window.cancelAnimationFrame )
window.cancelAnimationFrame = function( id ) {
clearTimeout( id );
};
}());
})(window);