1. 获取当前元素相对于document 的偏移量
    1. const getOffset = el => {
    2. const { top, left } = el.getBoundingClientRect()
    3. const { scrollTop, scrollLeft } = document.body
    4. return {
    5. top: top + scrollTop,
    6. left: left + scrollLeft,
    7. }
    8. }
    1. 获取元素类型
    1. const dataType = obj => Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, '$1').toLowerCase()
    1. 判断是否是移动端
    1. const isMobile = () => 'ontouchstart' in window
    1. 禁止网页复制粘贴
    1. const html = document.querySelector("html")
    2. html.oncopy = () => false
    3. html.onpaste = () => false
    1. 去除字符串中的html代码
    1. const removeHTML = (str = "") => str.replace(/<[\/\!]*[^<>]*>/ig, '')
    2. console.log(removeHTML("<h1>Hello World</h1>"))
    1. input 只能输入中文
    1. const input = document.querySelector('input')
    2. const clearText = target => {
    3. const {value} = target
    4. target.value = value.replace(/[^\u4e00-\u9fa5]/g, '')
    5. }
    6. input.onfocus = ({target}) => {clearText(target)}
    7. input.onblur = ({target}) => {clearText(target)}
    8. input.onkeyup = ({target}) => {clearText(target)}
    9. input.oninput = ({target}) => {clearText(target)}
    1. fade 动画
    1. const fade = (el, type = "in") => {
    2. el.style.opacity = (type === 'in') ? 0 : 1
    3. let last = +new Date()
    4. const tick = () => {
    5. const opacityValue = (type === 'in')
    6. ? (new Date() - last) / 400
    7. ? -(new Date() - last) / 400
    8. el.style.opacity = +el.style.opacity + opacityValue
    9. last = +new Date()
    10. if (type === 'in' ? (+el.style.opacity < 1) : (+el.style.opacity > 0)) {
    11. requestAnimationFrame(tick)
    12. }
    13. }
    14. tick()
    15. }
    1. 字符串后面空格去除和替换
    1. const trimEnd = str => str.replace(new RegExp('^(.*?)([\\s]*)$'), '$1')
    2. console.log(trimEnd("abc")) // abc
    3. console.log(trimEnd("123")) // 123
    1. 获取当前子元素是其父元素下子元素的排位
    1. const getIndex = el => {
    2. if (!el) return -1
    3. let index = 0;
    4. do {
    5. index++
    6. } while(el = el.previousElementSibling) // 返回当前元素在其父元素的子元素节点中的前一个元素节点,如果该元素已经是第一个元素节点,则返回null,该属性是只读的
    7. return index;
    8. }
    1. 字符串前面空格去除和替换
    1. const trimStart = str => str.replace(new RegExp('^([\\s]*)(.*?)$'), '$2');
    2. console.log(trimStart(" abc ")) // 'abc '
    3. console.log(trimStart("123 ")) // '123 '
    1. 函数柯里化
    1. const curring = fn => {
    2. const {length} = fn;
    3. const curried = (...args) => {
    4. return args.length >= length
    5. ? fn(...args)
    6. : (...args2) => curried(args.concat(args2))
    7. }
    8. return curried
    9. }
    10. const listMerge = (a,b,c) => [a, b,c]
    11. const curried = curring(listMerge)
    12. console.log(curried(1)(2)(3)) // [1,2,3]
    13. console.log(curried(1,2,3)) // [1,2,3]
    14. console.log(curried(1,2)(3)) // [1,2,3]
    1. 获取数组交集
    1. const intersection = (list, ...args) => list.fliter(item => args.every(list => list.includes(item)))
    2. console.log(intersection([2,1], [2,3])) // [2]
    3. console.log(intersection([1,2], [3,4])) // []
    1. 分割指定的长度的元素数组
    1. const listChunk = (list, size = 1, cacheList = []) => {
    2. const temp = [...list]
    3. if (size <= 0) {
    4. return cacheList
    5. }
    6. while(temp.length) {
    7. cacheList.push(temp.slice(0, size))
    8. }
    9. return cacheList
    10. }
    11. console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)) // [[1,2,3], [4,5,6,], [7,8,8]]
    12. console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 0)) // []
    1. 延迟函数delay
    1. const delay = ms => new Promise((resolve, reject) => setTimout(resolve, ms))
    2. const getData = status => new Promise((resolve, reject) => {
    3. status ? reslove('done') : reject('fail')
    4. })
    5. const getResult = async (data) => {
    6. try {
    7. const result = await getData(data)
    8. const timestamp = new Date().getTime
    9. await delay(1000)
    10. console.log(result, new Date().getTime() - timestamp)
    11. }catch(e) {
    12. console.log(e)
    13. }
    14. }
    15. getResult(true) // 隔了一秒
    1. 将指定格式字符串解析为日期字符串
    1. const dataPattern = (str, format = '-') => {
    2. if (!str) return new Date()
    3. const dateReg = new RegExp('^(\\d{2})${format}(\\d{2})${foramt}(\\d{4})$')
    4. const [, month, day, year] = dateReg.exec(dateReg)
    5. return new Date(`${month}, ${day} ${year}`)
    6. }
    7. console.log(datePattern('12-25-1995')) //Mon Dec 25 1995 00:00:00 GMT+0800 (中国标准时间

    参考
    [1] KRISACHAN: https://github.com/KRISACHAN
    [2]https://mp.weixin.qq.com/s/d8b90wcUltEKHq31ygKeIQ