inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 export function timeFix () {
I 2   const time = new Date()
3   const hour = time.getHours()
4   return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
5 }
6
7 export function welcome () {
8   const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 LOL', '我猜你可能累了']
9   const index = Math.floor(Math.random() * arr.length)
10   return arr[index]
11 }
12
13 /**
14  * 触发 window.resize
15  */
16 export function triggerWindowResizeEvent () {
17   const event = document.createEvent('HTMLEvents')
18   event.initEvent('resize', true, true)
19   event.eventType = 'message'
20   window.dispatchEvent(event)
21 }
22
23 export function handleScrollHeader (callback) {
24   let timer = 0
25
26   let beforeScrollTop = window.pageYOffset
27   callback = callback || function () {}
28   window.addEventListener(
29     'scroll',
30     event => {
31       clearTimeout(timer)
32       timer = setTimeout(() => {
33         let direction = 'up'
34         const afterScrollTop = window.pageYOffset
35         const delta = afterScrollTop - beforeScrollTop
36         if (delta === 0) {
37           return false
38         }
39         direction = delta > 0 ? 'down' : 'up'
40         callback(direction)
41         beforeScrollTop = afterScrollTop
42       }, 50)
43     },
44     false
45   )
46 }
47
48 export function isIE () {
49   const bw = window.navigator.userAgent
50   const compare = (s) => bw.indexOf(s) >= 0
51   const ie11 = (() => 'ActiveXObject' in window)()
52   return compare('MSIE') || ie11
53 }
54
55 /**
56  * Remove loading animate
57  * @param id parent element id or class
58  * @param timeout
59  */
60 export function removeLoadingAnimate (id = '', timeout = 1500) {
61   if (id === '') {
62     return
63   }
64   setTimeout(() => {
65     document.body.removeChild(document.getElementById(id))
66   }, timeout)
67 }