This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0
wcui/src/utils.js

139 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
* 公共方法库
2020-07-22 19:56:46 +08:00
* @author yutent<yutent.io@gmail.com>
* @date 2019/08/08 10:47:55
*/
function noop() {}
2019-12-19 01:04:15 +08:00
export default {
/**
* 异步回调
*/
nextTick: (function() {
let queue = []
2022-02-24 10:22:38 +08:00
let node = document.createTextNode('<!-- -->')
let bool = false
2019-12-19 01:04:15 +08:00
function callback() {
let n = queue.length
for (let i = 0; i < n; i++) {
queue[i]()
}
queue = queue.slice(n)
}
2019-12-19 01:04:15 +08:00
new MutationObserver(callback).observe(node, { characterData: true })
2019-12-19 01:04:15 +08:00
return function(fn) {
queue.push(fn)
bool = !bool
node.data = bool
}
})(),
2020-12-14 14:44:05 +08:00
//取得距离页面左上角的坐标
offset(node) {
try {
var rect = node.getBoundingClientRect()
if (rect.width || rect.height || node.getClientRects().length) {
var doc = node.ownerDocument
var root = doc.documentElement
var win = doc.defaultView
return {
top: rect.top + win.pageYOffset - root.clientTop,
left: rect.left + win.pageXOffset - root.clientLeft
}
}
} catch (e) {
return {
left: 0,
top: 0
}
}
},
2019-12-19 01:04:15 +08:00
/**
* 对象/数组遍历
* 支持跳出
*/
each(obj, fn) {
if (obj) {
if (Array.isArray(obj)) {
for (let i = 0, it; (it = obj[i++]); ) {
if (fn(it, i - 1) === false) {
break
}
}
2019-12-19 01:04:15 +08:00
} else {
for (let i in obj) {
if (obj.hasOwnProperty(i) && fn(obj[i], i) === false) {
break
}
}
}
}
2019-12-19 01:04:15 +08:00
},
2019-12-19 01:04:15 +08:00
/**
* 事件绑定
*/
bind(dom, type, fn = noop, phase = false) {
let events = type.split(',')
this.each(events, function(t) {
t = t.trim()
dom.addEventListener(t, fn, phase)
})
return fn
},
2019-12-19 01:04:15 +08:00
/**
* 事件绑定(默认不冒泡)
*/
catch(dom, type, fn, phase) {
function fn2(ev) {
2021-05-07 14:35:44 +08:00
ev.stopPropagation && ev.stopPropagation()
2019-12-19 01:04:15 +08:00
fn && fn(ev)
}
return this.bind(dom, type, fn2, phase)
},
2019-12-19 01:04:15 +08:00
/**
* 解除事件绑定
*/
unbind(dom, type, fn = noop, phase = false) {
let events = type.split(',')
this.each(events, function(t) {
t = t.trim()
dom.removeEventListener(t, fn, phase)
})
},
2019-08-19 14:15:18 +08:00
2019-12-19 01:04:15 +08:00
// 指定节点外点击(最高不能超过body层)
outside(dom, fn = noop) {
return this.bind(document, 'mousedown', ev => {
if (ev) {
if (ev.path) {
var path = ev.path.concat()
while (path.length > 3) {
if (path.shift() === dom) {
return
}
}
} else {
var target = ev.explicitOriginalTarget || ev.target
2022-02-24 10:22:38 +08:00
if (dom === target || dom.contains(target) || (dom.root && dom.root.contains(target))) {
2019-08-28 21:11:17 +08:00
return
}
}
2019-08-19 14:15:18 +08:00
}
2019-12-19 01:04:15 +08:00
fn(ev)
})
},
2020-12-14 14:44:05 +08:00
2019-12-19 01:04:15 +08:00
clearOutside(fn = noop) {
this.unbind(document, 'mousedown', fn)
}
2019-08-19 14:15:18 +08:00
}