-
Notifications
You must be signed in to change notification settings - Fork 0
/
wzpNameSpace.js
262 lines (247 loc) · 7.38 KB
/
wzpNameSpace.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
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
/*
* 命名空间
* ns_str 待创建的属性,例 "window.WZPUTILS"
* obj 待创建属性的值
*/
function namespace(ns_str, obj){
var parts = ns_str.split("."),
parent = window;
//剥离前面冗余的全局变量
if(parts[0] === "window"){
parts = parts.slice(1);
}
for(var i=0, len=parts.length; i<len; i++){
var part = parts[i];
if(typeof parent[part] === "undefined"){
parent[part] = (i === len - 1 ? obj : {});
}
parent = parent[part];
}
return parent;
}
namespace("window.wzpUtils", (function(){
var isNotIe = true, //是否IE8及以下,考虑兼容性
//子节点后插入
insertAfter = function(parentNode, newNode, existNode){
var nextNode = existNode.nextSibling;
if(nextNode){ //如果存在,在它前边插入
parentNode.insertBefore(newNode, nextNode);
}else{ //不存在,直接添加到最后
parentNode.appendChild(newNode);
}
},
//子节点倒序
reverseChildNodes = function(parentNode){
var childNodes = parentNode.childNodes,
firstChild = parentNode.firstChild,
lastIndex = childNodes.length - 1;
for(var i=lastIndex; i>0; i--){
parentNode.insertBefore(childNodes[lastIndex], firstChild);
}
},
//获取滚动条位置(兼容性)
getScrollOffset = (function(){
var func = function(){};
if(typeof window.pageXOffset === "number"){ //非IE8
func = function(){
return {
x: window.pageXOffset,
y: window.pageYOffset
};
};
}else{ //IE8
func = function(){
return {
x: document.body.scrollLeft + document.documentElement.scrollLeft,
y: document.body.scrollTop + document.documentElement.scrollTop
};
};
}
return func;
}()),
//获取窗口可视区尺寸(兼容性)
getViewportOffset = (function(){
var func = function(){};
if(typeof window.innerWidth === "number"){ //非ie8
func = function(){
return {
width: window.innerWidth,
height: window.innerHeight
};
};
}else if(document.compatMode === "CSS1Compat"){ //标准模式
func = function(){
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
};
};
}else if(document.compatMode === "BackCompat"){ //混杂模式
func = function(){
return {
width: document.body.clientWidth,
height: document.body.clientHeight
};
};
}else{
func = function(){
return {
width: 0,
height: 0
};
};
}
return func;
}()),
//任意元素相对文档位置(依赖getCSSStyle方法)
getElementPosition = function(elem){
var left = elem.offsetLeft,
top = elem.offsetTop,
offsetParent = elem.offsetParent;
if(offsetParent != document.body){
//如果offsetParent不是body,说明有position的父级
//递归继续去算父级偏移,并加上border宽度
var offset = this.getElementPosition(offsetParent), //获取父级偏移
cssStyle = this.getCSSStyle(offsetParent), //获取父级css
borderLeftWidth = parseInt(cssStyle.borderLeftWidth), //左border宽
borderTopWidth = parseInt(cssStyle.borderTopWidth); //上border宽
left += (borderLeftWidth + offset.left);
top += (borderTopWidth + offset.top);
}
return {
left: left,
top: top
};
},
//获取Css属性(兼容性)
getCSSStyle = (function(){
var func = function(){};
if(typeof window.getComputedStyle === "function"){ //非ie8
func = function(elem){
return window.getComputedStyle(elem, null);
}
}else{ //ie8
func = function(elem){
return elem.currentStyle;
}
}
return func;
}()),
//绑定事件(兼容性)
addEvent = (function(){
var func = function(){};
if(typeof Node.prototype.addEventListener === "function"){ //非ie8
func = function(elem, type, fn){
elem.addEventListener(type, fn, false);
}
}else if(typeof Node.prototype.attachEvent === "function"){ //ie8
func = function(elem, type, fn){
elem.attachEvent('on' + type, function(){
fn.call(elem); //IE的attachEvent事件中,this指向window
});
}
}else{
func = function(elem, type, fn){
elem['on' + type] = fn;
}
}
return func;
}()),
//解除绑定事件(兼容性)
removeEvent = (function(){
var func = function(){};
if(typeof Node.prototype.removeEventListener === "function"){ //非ie8
func = function(elem, type, fn){
elem.removeEventListener(type, fn, false);
}
}else if(typeof Node.prototype.detachEvent === "function"){ //ie8
func = function(elem, type, fn){
elem.detachEvent(type, fn);
}
}else{
func = function(elem, type){
elem['on' + type] = null;
}
}
return func;
}()),
//获取 事件源对象(兼容性)
getTarget = function(event){
return event.target || event.srcElement;
},
//取消冒泡(兼容性)
stopBubble = (function(){
var func = function(){};
if(typeof Event.prototype.stopPropagation === "function"){ //非ie8
func = function(event){
event.stopPropagation();
}
}else{ //ie8
func = function(event){
event.cancelBubble = true;
}
}
return func;
}()),
//取消默认事件(兼容性)
cancelDefault = (function(){
var func = function(){};
if(typeof Event.prototype.preventDefault === "function"){ //非ie8
func = function(event){
event.preventDefault();
}
}else{ //ie8
func = function(event){
event.returnValue = false;
}
}
return func;
}()),
//异步(按需)加载js(兼容性)
asyncJs = (function(){
var sc = document.createElement('script'),
func = function(){};
if(sc.onload){ //非ie8
func = function(src, callback){
var script = document.createElement('script');
script.type = "text/javascript";
script.onload = function(){
this.onload = null;
callback();
}
script.src = src; //这步执行,就会去下载src里的内容了,并且是异步下载
document.head.appendChild(script); //执行到这步,src下载的代码才会执行
}
}else{ //ie8
func = function(src, callback){
var script = document.createElement('script');
script.type = "text/javascript";
//IE,根据状态改变事件,监听状态码
script.onreadystatechange = function(){
var readyState = this.readyState;
if(readyState == "complete" || readyState == "loaded"){
this.onreadystatechange = null;
callback();
}
}
script.src = src; //这步执行,就会去下载src里的内容了,并且是异步下载
document.head.appendChild(script); //执行到这步,src下载的代码才会执行
}
}
return func;
}());
return {
insertAfter: insertAfter, //子节点后插入
reverseChildNodes: reverseChildNodes, //子节点倒序
getScrollOffset: getScrollOffset, //获取滚动条位置(兼容性)
getViewportOffset: getViewportOffset, //获取窗口可视区尺寸(兼容性)
getElementPosition: getElementPosition, //任意元素相对文档位置
getCSSStyle: getCSSStyle, //获取Css属性(兼容性)
addEvent: addEvent, //绑定事件(兼容性)
removeEvent: removeEvent, //解除绑定事件(兼容性)
getTarget: getTarget, //获取 事件源对象(兼容性)
stopBubble: stopBubble, //取消冒泡(兼容性)
cancelDefault: cancelDefault, //取消默认事件(兼容性)
asyncJs: asyncJs, //异步(按需)加载js(兼容性)
};
}()));