-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
88 lines (78 loc) · 2.35 KB
/
utils.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
window.utils = {
/**
* 根据父元素选择器获取父元素
*
* @param {Element} target
* @param {String} parentClass
* @returns {Element}
*/
getParentByClass (target, parentClass) {
parentClass = this.getStringBySelector(parentClass)
while (!this.hasClass(target, parentClass)) {
target = target.parentNode
}
return target
},
/**
* 去掉选择器选项
*
* @param {String} selector
* @returns {String}
*/
getStringBySelector (selector) {
if (/^[\.|#]/.test(selector)) {
selector = selector.substring(1)
}
return selector
},
/**
* 是否是子元素
*
* @param {Element} elem
* @param {String} parentClassString
* @returns {Boolean}
*/
isChild (elem, parentClassString) {
let checkNode = elem,
isDocumentElem = false,
isChild = this.hasClass(checkNode, parentClassString)
while (!isChild && !isDocumentElem) {
if (checkNode.tagName === 'HTML') {
isDocumentElem = true
}
checkNode = checkNode.parentNode
isChild = this.hasClass(checkNode, parentClassString)
}
return isChild
},
hasClass (elem, classString) {
if (elem && elem.classList) {
return elem.classList.contains(classString)
} else {
return false
}
},
/**
* 事件委托
*
* @param {String} event 事件类型
* @param {Element} rootElem 根元素
* @param {String|Array} bindElemSelector 被绑定元素的选择器
* @param {function} callback 触发回调
*/
on (event, rootElem, bindElemSelector, callback) {
const _checkBind = (selectString, e) => {
if (this.hasClass(e.target, selectString) || this.isChild(e.target, selectString)) {
// console.log(bindElemSelector)
callback && callback(e)
}
}
rootElem.addEventListener(event, e => {
if (bindElemSelector instanceof Array) {
bindElemSelector.forEach(selector => _checkBind(this.getStringBySelector(selector), e))
} else {
_checkBind(bindElemSelector.substring(1), e)
}
}, false)
}
}