-
Notifications
You must be signed in to change notification settings - Fork 11
/
relocate.js
47 lines (46 loc) · 1.48 KB
/
relocate.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
/**
* Use relocat() to move HTML elements in the DOM
* above a certain window width.
*
* Copyright by Eike Send, Edenspiekermann AG
*
* Licensed under the GPLv2 and the MIT license
*
*/
relocate = function(width, elements, destinationElement, desktopFirst) {
// ensure that we use an array-like argument, NodeList and HTMLCollection work as well
if (elements.nodeName) elements = [elements];
var placeHolders = [],
els = [],
parentEl, el, i,
l = elements.length;
// first, create a non-live copy of the elements
// this avoids nasty bugs when elements are removed and added again
for (i = l-1; i >= 0; i--) {
els.push(elements[i]);
}
var forwardFunction = function() {
for (i = 0; i < l; i++) {
parentEl = els[i].parentNode;
if (placeHolders[i] === undefined) {
placeHolders[i] = document.createElement("span");
parentEl.insertBefore(placeHolders[i], els[i]);
}
el = parentEl.removeChild(els[i]);
destinationElement.insertBefore(el, destinationElement.firstChild);
}
}
var backwardFunction = function() {
for (i = 0; i < l; i++) {
parentEl = els[i].parentNode;
el = parentEl.removeChild(els[i]);
placeHolders[i].parentNode.insertBefore(el, placeHolders[i]);
}
}
// then create a object that operates on it:
if (!desktopFirst) {
minwidth(width, forwardFunction, backwardFunction);
} else {
minwidth(width, backwardFunction, forwardFunction, desktopFirst);
}
}