-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
120 lines (97 loc) · 3.63 KB
/
main.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
(function(){
/**
*
* Создаем новый элемент и добавляем на страницу
*
*/
function create() {
const collectionQueryElenent = document.querySelectorAll('.query');
const appElenent = document.querySelector('.App');
const divResult = document.createElement('div');
const textNode = document.createTextNode(`element count ${collectionQueryElenent.length}`);
divResult.setAttribute('class', 'result');
divResult.appendChild(textNode);
const fragment = document.createDocumentFragment();
for(let i = 0; i < 100; i++) {
const div = document.createElement('div');
div.style.background = 'red';
div.style.width = '100px';
div.style.height = '100px';
div.style.marginBottom = '10px';
fragment.appendChild(div);
}
appElenent.appendChild(fragment);
//console.log(appElenent.nodeType, appElenent.nodeName, appElenent.nodeValue);
}
//create();
/**
*
* Обход DOM дерева
*
*/
function recursion() {
const child = document.getElementById('ChildBlock');
const hypnoticToad = document.querySelector('.hypnoticToad');
const img = new Image(363, 314);
img.src = 'http://i.absurdopedia.net/4/48/Hypno_large.gif';
let count = 0;
const attempt = 2;
function drawGreen(el) {
el.style.border = '2px solid green';
}
function drawBlack(el) {
el.style.border = '5px solid #000';
}
/**
* Подъем вверх по дереву
*
* */
function catchParent(el) {
drawGreen(el);
const parent = el.parentNode;
const id = parent.getAttribute('id')
if(!id) {
setTimeout(() => {
drawGreen(parent);
catchParent(parent)
}, 300);
} else {
decorateChildren(parent);
drawGreen(parent)
};
}
/**
* Проход вниз по девеву
*
* */
function decorateChildren(el) {
// фильтруем дочерные элементы, так как там может быть символ переноса строки
// берем первый элемент из массива [0]
const child = filterChild(el)[0];
if(count === attempt) {
hypnoticToad.appendChild(img);
return;
}
if(child) {
setTimeout(() => {
drawBlack(child);
decorateChildren(child);
}, 300)
} else {
count++;
setTimeout(() => {
drawBlack(el);
catchParent(el);
}, 300)
}
}
// child.style.webkitTransform = "rotate(-2deg)";
catchParent(child);
}
// recursion();
function filterChild(el) {
const collection = Array.from(el.childNodes);
return collection.filter(c => c.tagName === 'DIV')
}
// child();
}());