util.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /**
  44. * Remove loading animate
  45. * @param id parent element id or class
  46. * @param timeout
  47. */
  48. export function removeLoadingAnimate (id = '', timeout = 1500) {
  49. if (id === '') {
  50. return
  51. }
  52. setTimeout(() => {
  53. document.body.removeChild(document.getElementById(id))
  54. }, timeout)
  55. }
  56. /**
  57. * @param {String|Number} value 要验证的字符串或数值
  58. * @param {*} validList 用来验证的列表
  59. */
  60. export function oneOf (value, validList) {
  61. for (let i = 0; i < validList.length; i++) {
  62. if (value === validList[i]) {
  63. return true
  64. }
  65. }
  66. return false
  67. }