国产化查询机
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

7 months ago
  1. export default {
  2. data() {
  3. return {
  4. scrollDom: null,
  5. interval: null,
  6. scrollTimer: null, // 滚动定时器
  7. pauseTimer: null, // 暂停定时器
  8. timeout: null,
  9. step: null
  10. }
  11. },
  12. created() {
  13. },
  14. mounted() {
  15. // this.dataCompleteFun()
  16. },
  17. destroyed() {
  18. // 清理定时器
  19. clearTimeout(this.pauseTimer)
  20. this.pauseTimer = null
  21. clearInterval(this.scrollTimer)
  22. this.scrollTimer = null
  23. // 清理点击监听
  24. window.document.removeEventListener('click', this.pauseScroll)
  25. },
  26. methods: {
  27. autoScroll() {
  28. // 滚动长度为0
  29. if (this.scrollDom.scrollHeight - this.scrollDom.clientHeight > 0) {
  30. // 如果定时器存在
  31. if (this.scrollTimer) {
  32. // 则先清除
  33. clearInterval(this.scrollTimer)
  34. clearTimeout(this.pauseTimer)
  35. this.scrollTimer = null
  36. this.pauseTimer = null
  37. }
  38. this.scrollTimer = setInterval(() => {
  39. const scrollHeight = this.scrollDom.scrollHeight
  40. const clientHeight = this.scrollDom.clientHeight
  41. const scroll = scrollHeight - clientHeight
  42. // 获取当前滚动条距离顶部高度
  43. const scrollTop = this.scrollDom.scrollTop
  44. // 当滚动到底部时,间隔时间后重回顶部开始
  45. if (scrollTop + this.step >= scroll) {
  46. this.scrollDom.scrollTop = scroll
  47. this.pauseTimer = setTimeout(() => {
  48. this.scrollDom.scrollTop = 0
  49. this.autoScroll()
  50. }, this.timeout)
  51. } else { // 没有则继续滚动
  52. this.scrollDom.scrollTop = scrollTop + this.step
  53. }
  54. // console.log(scrollHeight, clientHeight, scroll, scrollTop)
  55. }, this.interval)
  56. } else {
  57. return
  58. }
  59. }
  60. }
  61. }