forked from WangJia-mm/JavaScript201708
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils201708.js
More file actions
309 lines (281 loc) · 9.93 KB
/
utils201708.js
File metadata and controls
309 lines (281 loc) · 9.93 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//->utils:common method libraries used in projects
var utils = (function () {
var isHighVersion = 'getComputedStyle' in window;
//->toArray:converts a like array into an array
function toArray(likeAry) {
var ary = [];
if (isHighVersion) {
ary = [].slice.call(likeAry);
} else {
for (var i = 0; i < likeAry.length; i++) {
ary[ary.length] = likeAry[i];
}
}
return ary;
}
//->toJSON:converts a string to a JSON object
function toJSON(str) {
return 'JSON' in window ? JSON.parse(str) : eval('(' + str + ')');
}
//->getCss:gets the style property value of the element
function getCss(curEle, attr) {
var val = null;
if (isHighVersion) {
val = window.getComputedStyle(curEle, null)[attr];
} else {
if (attr.toLowerCase() === 'opacity') {
val = curEle.currentStyle['filter'];
reg = /^alpha\(opacity=(.+)\)$/i;
val = reg.test(val) ? reg.exec(val)[1] / 100 : 1;
} else {
val = curEle.currentStyle[attr];
}
}
var temp = parseFloat(val);
val = isNaN(temp) ? val : temp;
return val;
}
//->setCss:sets the style property value of an element
function setCss(curEle, attr, value) {
if (attr.toLowerCase() === 'opacity') {
curEle.style.opacity = value;
curEle.style.filter = 'alpha(opacity=' + (value * 100) + ')';
return;
}
var unitReg = /^(zIndex|fontWeight)$/i;
if (!isNaN(value) && !unitReg.test(attr)) {
value += 'px';
}
curEle['style'][attr] = value;
}
//->setGroupCss:sets the style attribute values of elements in batch settings
function setGroupCss(curEle, options) {
if (Object.prototype.toString.call(options) !== '[object Object]') return;
for (var attr in options) {
if (options.hasOwnProperty(attr)) {
setCss(curEle, attr, options[attr]);
}
}
}
//->css:the style attributes of the operation element include settings individually, batch settings, access styles, and so on
function css() {
var arg = arguments,
len = arg.length,
fn = getCss;
if (len >= 3) fn = setCss;
if (len === 2 && typeof arg[1] === 'object') fn = setGroupCss;
return fn.apply(null, arg);
}
//->offset:gets the offset of the current element distance BODY => {left:xxx,top:xxx}
function offset(curEle) {
var l = curEle.offsetLeft,
t = curEle.offsetTop,
p = curEle.offsetParent;
while (p && p !== document.body) {
if (!/MSIE 8/i.test(navigator.userAgent)) {
l += p.clientLeft;
t += p.clientTop;
}
l += p.offsetLeft;
t += p.offsetTop;
p = p.offsetParent;
}
return {left: l, top: t};
}
//->win:operate the box model properties about the browser
function win(attr, value) {
if (typeof value !== 'undefined') {
document.documentElement[attr] = value;
document.body[attr] = value;
return;
}
return document.documentElement[attr] || document.body[attr];
}
//->prev:gets the last brother element node
function prev(curEle) {
if (isHighVersion) {
return curEle.previousElementSibling;
}
var p = curEle.previousSibling;
while (p && p.nodeType !== 1) {
p = p.previousSibling;
}
return p;
}
//->next:gets the next brother element node
function next(curEle) {
if (isHighVersion) return curEle.nextElementSibling;
var n = curEle.nextSibling;
while (n && n.nodeType !== 1) {
n = n.nextSibling;
}
return n;
}
//->prevAll:gets all brother element nodes
function prevAll(curEle) {
var ary = [],
p = curEle.previousSibling;
while (p) {
if (p.nodeType === 1) {
ary.unshift(p);
}
p = p.previousSibling;
}
return ary;
}
//->nextAll:gets all the brother element nodes
function nextAll(curEle) {
var ary = [],
n = curEle.nextSibling;
while (n) {
if (n.nodeType === 1) {
ary.push(n);
}
n = n.nextSibling;
}
return ary;
}
//->siblings:gets all sibling element nodes
function siblings(curEle) {
return prevAll(curEle).concat(nextAll(curEle));
}
//->index:gets the index of the current element
function index(curEle) {
return prevAll(curEle).length;
}
//->firstChild:gets the first element child node of the current container
function firstChild(curEle) {
if (isHighVersion) return curEle.firstElementChild;
var f = curEle.firstChild;
while (f && f.nodeType !== 1) {
f = f.nextSibling;
}
return f;
}
//->lastChild:gets the last element child node of the current container
function lastChild(curEle) {
if (isHighVersion) return curEle.lastElementChild;
var l = curEle.lastChild;
while (l && l.nodeType !== 1) {
l = l.previousSibling;
}
return l;
}
//->children:gets all element child nodes of the container
function children(curEle, tag) {
var ary = [],
nodeList = curEle.childNodes;
for (var i = 0; i < nodeList.length; i++) {
var cur = nodeList[i];
if (cur.nodeType === 1) {
if (typeof tag !== 'undefined') {
cur.tagName.toUpperCase() === tag.toUpperCase() ? ary.push(cur) : null;
continue;
}
ary.push(cur);
}
}
return ary;
}
//->byClass:getElementsByClassName compliant processing, in the specified context, gets the elements that have these style classes by passing the style name class name
function byClass(strClass, context) {
context = context || document;
if (isHighVersion) return [].slice.call(context.getElementsByClassName(strClass));
strClass = strClass.replace(/^\s+|\s+$/g, '').split(/\s+/g);
var tagList = context.getElementsByTagName('*'),
result = [];
for (var i = 0; i < tagList.length; i++) {
var item = tagList[i],
itemClass = item.className;
var isMatch = true;
for (var k = 0; k < strClass.length; k++) {
var reg = new RegExp('(^| +)' + strClass[k] + '( +|$)');
if (!reg.test(itemClass)) {
isMatch = false;
break;
}
}
isMatch ? result.push(item) : null;
}
return result;
}
//->hasClass:verify that the current element has a style name
function hasClass(curEle, strClass) {
strClass = strClass.replace(/^\s+|\s+$/g, '').split(/\s+/g);
var isMatch = true,
originalClass = curEle.className;
for (var i = 0; i < strClass.length; i++) {
var item = strClass[i];
var reg = new RegExp('(^| +)' + item + '( +|$)');
if (!reg.test(originalClass)) {
isMatch = false;
break;
}
}
return isMatch;
}
//->addClass:increase the element's style name class name
function addClass(curEle, strClass) {
strClass = strClass.replace(/^\s+|\s+$/g, '').split(/\s+/g);
for (var i = 0; i < strClass.length; i++) {
var item = strClass[i];
if (hasClass(curEle, item)) continue;
curEle.className += ' ' + item;
}
curEle.className = curEle.className.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
}
//->removeClass:removes the name of the element's style class name
function removeClass(curEle, strClass) {
strClass = strClass.replace(/^\s+|\s+$/g, '').split(/\s+/g);
var originalClass = curEle.className.replace(/\s+/g, ' ');
for (var i = 0; i < strClass.length; i++) {
var item = strClass[i],
reg = new RegExp('(^| )' + item + '( |$)', 'g');
reg.test(originalClass) ? originalClass = originalClass.replace(reg, ' ') : null;
}
curEle.className = originalClass.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
}
//->append:appends elements at the end of the container
function append(curEle, parent) {
parent.appendChild(curEle);
}
//->prepend:appends elements to the beginning of the container
function prepend(curEle, parent) {
var first = firstChild(parent);
first ? parent.insertBefore(curEle, first) : parent.appendChild(curEle);
}
//->insertBefore:before appending to the specified element
function insertBefore(curEle, oldEle) {
oldEle.parentNode.insertBefore(curEle, oldEle);
}
//->insertAfter:after appending to the specified element
function insertAfter(curEle, oldEle) {
var nex = next(oldEle),
p = oldEle.parentNode;
nex ? p.insertBefore(curEle, nex) : p.appendChild(curEle);
}
return {
toArray: toArray,
toJSON: toJSON,
css: css,
offset: offset,
win: win,
prev: prev,
next: next,
prevAll: prevAll,
nextAll: nextAll,
siblings: siblings,
index: index,
firstChild: firstChild,
lastChild: lastChild,
children: children,
byClass: byClass,
hasClass: hasClass,
addClass: addClass,
removeClass: removeClass,
append: append,
prepend: prepend,
insertBefore: insertBefore,
insertAfter: insertAfter
}
})();