forked from WangJia-mm/JavaScript201708
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
109 lines (100 loc) · 3.44 KB
/
utils.js
File metadata and controls
109 lines (100 loc) · 3.44 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
//->utils:common method libraries used in projects
var utils = (function () {
//->toArray:converts a like array into an array
function toArray(likeAry) {
var ary = [];
try {
ary = [].slice.call(likeAry);
} catch (e) {
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 ('getComputedStyle' in window) {
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];
}
return {
toArray: toArray,
toJSON: toJSON,
css: css,
offset: offset,
win: win
}
})();