-
Notifications
You must be signed in to change notification settings - Fork 2
/
vesponsive.js
155 lines (132 loc) · 3.82 KB
/
vesponsive.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
var Vesponsive = (function(root, undefined) {
"use strict";
// Private
var _this;
var toArray = Array.prototype.slice;
var listener = window.addEventListener;
function init(opts) {
// Return if on old IE
if('attachEvent' in window) {
return false;
}
_this = this;
_this.opts = opts;
_this.elems = [];
addListeners();
};
function addListeners() {
// Listen
if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") {
setElems();
} else {
listener('DOMContentLoaded', setElems);
}
listener('resize', resizeHandler);
};
function setElems() {
var elems = _this.opts.elems;
elems.forEach(function(elem, i) {
var node = DOM.getElem(elem.selector);
var receiverNode = DOM.getElem(elem.receiverSelector);
if(!node || !receiverNode) {
return;
}
if (Array.isArray(node)) {
node = node[0];
}
_this.elems.push({
node: node,
breakPoint: elem.breakpoint,
receiver: Array.isArray(receiverNode) ? receiverNode[0] : receiverNode,
isMoved: false,
origPrevSibling: node.previousElementSibling,
origParent: node.parentNode,
priority: i,
DOMmethod: elem.position,
additionalClass: elem.additionalClass
});
});
// Set init positions
resizeHandler();
};
function resizeHandler() {
var elems = _this.elems;
var width = window.innerWidth;
elems.forEach(function(elem, i) {
if(!elem.node || !elem.receiver) {
return;
}
var breakPoint = parseInt( elem.breakPoint, 10);
if( !elem.isMoved && width <= breakPoint ) {
elem.isMoved = true;
if(elem.additionalClass && elem.additionalClass.length) {
DOM.addClass(elem.node, elem.additionalClass);
}
DOM[elem.DOMmethod](elem.receiver, elem.node);
} else if( elem.isMoved && width > breakPoint ) {
elem.isMoved = false;
if(elem.origPrevSibling) {
DOM.after(elem.origPrevSibling, elem.node);
} else {
DOM.prepend(elem.origParent, elem.node);
}
if(elem.additionalClass && elem.additionalClass.length) {
DOM.removeClass(elem.node, elem.additionalClass);
}
}
});
}
// Helpers
var DOM = {
/*
* Returns the DOM-node based on selector
*/
getElem: function(selector) {
if(~selector.indexOf('#') && !~selector.indexOf('>')) {
return document.getElementById(selector.substr(1));
} else {
return toArray.call(document.querySelectorAll(selector)) || [];
}
},
/*
* Append newNode into referenceNode
*/
append: function(referenceNode, newNode) {
referenceNode.appendChild(newNode);
},
/*
* Perpend newNode into referenceNode
*/
prepend: function(referenceNode, newNode) {
referenceNode.insertBefore( newNode, referenceNode.firstChild);
},
/*
* Insert newNode after referenceNode
*/
after: function(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
},
/*
* Insert newNode before referenceNode
*/
before: function(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode);
},
addClass: function(node, name) {
if(node.className.indexOf(name) !== -1) {
return;
}
var pre = node.className.length > 0 ? ' ' : '';
node.className += pre + name;
},
removeClass: function(node, name) {
node.className = node.className.replace(name, '');
}
};
// Public
return {
init: init,
triggerResize: resizeHandler,
version: '1.1'
};
})(window);