util.js 1.7 KB

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