-
Notifications
You must be signed in to change notification settings - Fork 2
/
qin.js
98 lines (91 loc) · 2.53 KB
/
qin.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
/**
* 需要浏览器支持HTML node对象及querySelect方法
* @param {[document]} document [document对象]
* @param {[window]} window [window对象]
*/
;
(function(document, window) {
var node = Node.prototype,
trigger = 'trigger',
// 兼容firefox, firefox下创建元素需要一个字符串作为参数
dummy = document.createElement('i');
//为nodeList添加遍历方法
NodeList.prototype.forEach = [].forEach
//为node对象添加on方法
window.on = Node.prototype.on = function(event, fn) {
this.addEventListener(event, fn, false);
return this;
};
//nodeList遍历添加
NodeList.prototype.on = function(event, fn) {
this.forEach(function(el) {
el.on(event, fn);
});
return this;
};
//封装node style属性
Node.prototype.css = function(k, v) {
var self = this;
typeof k != 'object' ? this.style[k] = v : (function(k) {
for (var i in k) {
self.css(i, k[i])
}
})(k)
return this;
}
//同理递归实现
NodeList.prototype.css = function(k, v) {
this.forEach(
function(el) {
el.css(k, v)
}
)
return this;
}
//添加自定义事件支持
window[trigger] = Node.prototype[trigger] = function(type, data) {
//模拟事件
var event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.data = data || {};
event.eventName = type;
event.target = this;
//触发事件
this.dispatchEvent(event);
//链式调用
return this;
};
//遍历调用
NodeList.prototype[trigger] = function(event) {
this.forEach(function(el) {
el[trigger](event);
});
return this;
};
//创建实例 s 为选择器元素
var entry = function(s) {
//获取node对象
var r = document.querySelectorAll(s || 'body'),
length = r.length;
//单个元素直接返回,多个元素返回nodeList
return length == 1 ? r[0] : r;
};
//获取node.on(),重新指定作用域,完成自定义事件支持
entry.on = Node.prototype.on;
entry[trigger] = Node.prototype[trigger];
//模块化
if (typeof exports !== 'undefined' && module.exports) {
module.exports = exports = entry;
} else if (typeof define === 'function' && define.cmd) {
define(function(require, exports, module) {
module.exports = exports = entry;
})
} else if (typeof define === 'function' && define.amd) {
define('qin', [], function() {
return entry
});
} else {
//浏览器端直接运行
window.$ = entry;
}
})(document, window);