函数工具合集
export default class Utils {
constructor () {
}
static ce(type, style, data, prop) {
let elem = document.createElement(type);
if (style) Object.assign(elem.style, style);
if (data) Object.assign(elem, data);
if (prop) {
for (let k in prop) {
elem.setAttribute(k, prop[k]);
}
}
return elem;
}
static randomColor () {
let c = "#";
for (let i = 0; i < 6; i++) {
c += Math.floor(Math.random() * 16).toString(16);
}
return c;
}
static random (min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
}
ce方法,创建DOM方法,可以给DOM添加样式对象,对象属性,标签属性
Utils.ce("div", {
width: "50px",
height: "50px",
backgroundColor: "red"
}, {
textContent: "hello",
a: 2
}, {
class: "div0 div1",
title: "div"
})
randomColor获取随机色
let color = Utils.randomColor();
random随机求最小值到最大值之间的整数
let color = Utils.random();