/
helpers.js
87 lines (73 loc) · 2.01 KB
/
helpers.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
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[key] = b[key];
}
}
return a;
}
function getContainerWidth(container){
if(typeof container !== 'undefined' && container){
return container.offsetWidth;
}else{
return window.innerWidth || document.documentElement.clientWidth;
}
}
function getContainerHeight(container){
if(typeof container !== 'undefined' && container){
return container.offsetHeight;
}else{
return window.innerHeight || document.documentElement.clientHeight;
}
}
function each(object, fn){
if(object && fn) {
var l = object.length;
for(var i = 0; i<l && fn(object[i], i) !== false; i++){}
}
}
function intVal(x){
if(x){
return parseInt(x, 10);
}else{
return 0;
}
}
function getLeftOffset(element , multiplier){
return intVal(- (getContainerWidth(element) * multiplier));
}
function getContainer(el, parentClass){
while (el && el.parentNode) {
el = el.parentNode;
if (el.className === parentClass) {
return el;
}
}
// Many DOM methods return null if they don't
// find the element they are searching for
// It would be OK to omit the following and just
// return undefined
return null;
}
function isElementLoaded(ele, successClass) {
return classie.has(ele, successClass);
}
/**
* Determine if an element is in the viewport, from:
* http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433
*
* @param {HTMLElement} el
*/
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}