开发常用的工具函数

  1.时间格式化

  第三方库:day.jsmoment.js,以下为手搓简易版(功能少偶尔用)

 1 export const useFormatDate = (
 2   date: Date,
 3   firtType: string = '-',
 4   secondType: string = ':',
 5   centerSlice: string = ' ',
 6 ) => {
 7   const year = String(date.getFullYear())
 8   let month = String(date.getMonth() + 1)
 9   let day = String(date.getDate())
10   let hour = String(date.getHours())
11   let minute = String(date.getMinutes())
12   let second = String(date.getSeconds())
13   if (month.length == 1) month = '' + 0 + month
14   if (day.length == 1) day = '' + 0 + day
15   if (hour.length == 1) hour = '' + 0 + hour
16   if (minute.length == 1) minute = '' + 0 + minute
17   if (second.length == 1) second = '' + 0 + second
18   let formatRes =
19     year +
20     firtType +
21     month +
22     firtType +
23     day +
24     centerSlice +
25     hour +
26     secondType +
27     minute +
28     secondType +
29     second
30   return {
31     year,
32     month,
33     day,
34     hour,
35     minute,
36     second,
37     formatRes,
38   }
39 }

  2.数字滚动效果

  第三方库:CountUp.js,以下为自己封装的简易版(功能还不完善)

 1 export const useCountUp = (el: HTMLElement, wait: number = 20) => {
 2   let counter = Number(String(el.innerText))
 3   let nowCount: any = 0
 4   let integer = true
 5   let timer: any = null
 6   let step: number = 10
 7 
 8   const arr = String(counter).split('')
 9   arr.forEach((item: string) => {
10     if (item == '.') {
11       integer = false
12     }
13   })
14 
15   // console.log(integer)
16 
17   for (let i = 0; i <= 55; i++) {
18     timer = setTimeout(() => {
19       if (el) {
20         // console.log(i * counter * 0.02)
21         nowCount = i * counter * 0.02 <= counter ? i * counter * 0.02 : counter
22         if (integer) {
23           nowCount = nowCount.toFixed(0)
24         } else {
25           nowCount = nowCount.toFixed(2)
26         }
27 
28         el.innerHTML = nowCount
29       }
30     }, step)
31     step += wait
32   }
33   clearTimeout(timer)
34   // import setTimeout是异步,延迟时间需递增
35 }

  3.Scoll(监控元素是否触定和触底)

 1 import { ref } from 'vue'
 2 
 3 const useScoll = (el: HTMLElement, time: number = 50) => {
 4   let isTop = ref<boolean>()
 5   let isBottom = ref<boolean>()
 6   let timer: any = null
 7 
 8   function scollFn() {
 9     clearTimeout(timer)
10     timer = setTimeout(() => {
11       const scollTop = el.scrollTop
12       const scollHeight = el.scrollHeight
13       const clientHeight = el.clientHeight
14       isTop.value = scollTop <= 50 ? true : false
15       isBottom.value = scollTop + clientHeight + 20 >= scollHeight
16     }, time)
17   }
18   scollFn()
19 
20   function offScoll() {
21     el.removeEventListener('scroll', scollFn)
22   }
23 
24   el.addEventListener('scroll', scollFn)
25 
26   return {
27     isTop,
28     isBottom,
29     offScoll,
30   }
31 }
32 
33 export default useScoll

  4.读取input上传的本地图片(转成base64)

 1 export const useReadFilePath = (file: any) => {
 2   return new Promise((resolve, reject) => {
 3     const reader = new FileReader()
 4     reader.readAsDataURL(file)
 5     reader.onloadend = (res: any) => {
 6       const result = res.target.result
 7       resolve(result)
 8     }
 9   })
10 }

  5.图片懒加载

 1 import { ref } from 'vue'
 2 
 3 const useInClient = (el: HTMLElement, imgEl: HTMLImageElement, time: number = 50) => {
 4   let isIn = ref<boolean>(false)
 5   let timer: any = ''
 6 
 7   function scollFn() {
 8     clearTimeout(timer)
 9     timer = setTimeout(() => {
10       const scollTop = el.scrollTop
11       const offset = imgEl.offsetTop
12       const clientHeight = el.clientHeight
13       //
14       isIn.value = scollTop + clientHeight >= offset - 50 ? true : false
15       if (isIn.value == true) {
16         imgEl.src = imgEl.getAttribute('data-url') as string
17       }
18       console.log(isIn.value)
19     }, time)
20   }
21   scollFn()
22 
23   function offScoll() {
24     el.removeEventListener('scroll', scollFn)
25   }
26 
27   el.addEventListener('scroll', scollFn)
28 
29   return {
30     isIn,
31     offScoll,
32   }
33 }
34 
35 export default useInClient

  6.防抖和节流

  1.每次进入函数后先清除定时器,再重新赋值定时器,可防止scoll、resize等频繁触发

  2.每次进入函数后先判断有无定时器,有则return退出函数,没有则进入函数执行业务,可防止用户快速多次点击业务按钮

 

 

posted @ 2023-01-26 20:17  january-yy  阅读(17)  评论(0)    收藏  举报