class Util {
// 随机函数
static rand = (min, max) => Math.round(Math.random() * (max-min) + min)
// 随机颜色
static randColor = (isAlpha) => `rgba(${Util.rand(0, 255)},${Util.rand(0, 255)},${Util.rand(0, 255)},${isAlpha ? Math.random() : 1})`
// 碰撞检测 a b是两个dom元素
static isCheck = (a, b) => {
let l1 = a.offsetLeft;
let t1 = a.offsetTop;
let r1 = l1 + a.offsetWidth;
let b1 = t1 + a.offsetHeight;
let l2 = b.offsetLeft;
let t2 = b.offsetTop;
let r2 = l2 + b.offsetWidth;
let b2 = t2 + b.offsetHeight;
return !(r1<l2 || r2<l1 || b2<t1 || b1<t2)
}
// 是否是移动端
static isMobile = () => ['Android', 'HarmonyOS', 'iPhone', 'iPad'].some(item => navigator.userAgent.includes(item))
// 深拷贝
static deepCopy = data => JSON.parse(JSON.stringify(data))
}
封装一些函数
最新推荐文章于 2024-10-28 15:18:39 发布