SeamlessScroll.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="seamless-scroll-container" ref="scrollRef" @mouseenter="pause" @mouseleave="resume">
  3. <slot :list="scrollData"></slot>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'SeamlessScroll',
  9. props: {
  10. data: { type: Array, required: true },
  11. limit: { type: Number, default: 4 },
  12. speed: { type: Number, default: 0.5 },
  13. measureSelector: { type: String, default: 'tbody' }
  14. },
  15. data() {
  16. return {
  17. scrollTimer: null,
  18. currentTop: 0,
  19. resetHeight: 0,
  20. isScrollable: false
  21. }
  22. },
  23. computed: {
  24. scrollData() {
  25. if (this.data && this.data.length > this.limit) {
  26. this.isScrollable = true;
  27. const clone = JSON.parse(JSON.stringify(this.data)).map((item, index) => ({
  28. ...item,
  29. _clone_id: `clone_${Date.now()}_${index}`
  30. }));
  31. return [...this.data, ...clone];
  32. }
  33. this.isScrollable = false;
  34. return this.data;
  35. }
  36. },
  37. // 加入 mounted 钩子,确保 DOM 绝对渲染完毕再测算
  38. mounted() {
  39. console.log('✅ SeamlessScroll: 组件已挂载,准备初始化滚动');
  40. this.initScroll();
  41. },
  42. watch: {
  43. data: {
  44. handler() {
  45. console.log('🔄 SeamlessScroll: 监测到数据变化');
  46. this.initScroll();
  47. },
  48. deep: true
  49. }
  50. },
  51. beforeDestroy() {
  52. this.pause();
  53. },
  54. methods: {
  55. initScroll() {
  56. this.pause();
  57. this.currentTop = 0;
  58. if (this.$refs.scrollRef) this.$refs.scrollRef.scrollTop = 0;
  59. if (!this.isScrollable) {
  60. console.log('🛑 SeamlessScroll: 数据量不足,无需滚动');
  61. return;
  62. }
  63. this.$nextTick(() => {
  64. // 加大一点延时,给表格充足的撑开时间
  65. setTimeout(() => {
  66. const wrapper = this.$refs.scrollRef;
  67. if (!wrapper) return;
  68. const measureEl = wrapper.querySelector(this.measureSelector);
  69. // 【排查神器】:打印高度对比
  70. // console.log(`📏 尺寸核对 -> 容器可视高度: ${wrapper.clientHeight}px, 内容真实总高: ${wrapper.scrollHeight}px`);
  71. // 如果容器高度等于或大于内容高度,说明没有溢出,肯定滚不动
  72. if (wrapper.scrollHeight <= wrapper.clientHeight) {
  73. console.warn('⚠️ SeamlessScroll 警告: 内容高度没有超出容器高度,滚动被迫终止!请检查外部 CSS 高度限制。');
  74. return;
  75. }
  76. this.resetHeight = measureEl ? measureEl.offsetHeight / 2 : wrapper.scrollHeight / 2;
  77. // console.log('🚀 SeamlessScroll: 滚动初始化成功!复位锚点高度为:', this.resetHeight);
  78. this.resume();
  79. }, 100);
  80. });
  81. },
  82. resume() {
  83. if (!this.isScrollable || this.resetHeight <= 0) return;
  84. const step = () => {
  85. const wrapper = this.$refs.scrollRef;
  86. if (!wrapper) return;
  87. this.currentTop += this.speed;
  88. wrapper.scrollTop = Math.floor(this.currentTop);
  89. if (wrapper.scrollTop >= this.resetHeight) {
  90. this.currentTop = 0;
  91. wrapper.scrollTop = 0;
  92. }
  93. this.scrollTimer = requestAnimationFrame(step);
  94. };
  95. this.scrollTimer = requestAnimationFrame(step);
  96. },
  97. pause() {
  98. if (this.scrollTimer) {
  99. cancelAnimationFrame(this.scrollTimer);
  100. this.scrollTimer = null;
  101. }
  102. }
  103. }
  104. }
  105. </script>
  106. <style scoped>
  107. .seamless-scroll-container {
  108. width: 100%;
  109. height: 100%;
  110. overflow: hidden;
  111. scrollbar-width: none;
  112. -ms-overflow-style: none;
  113. }
  114. .seamless-scroll-container::-webkit-scrollbar {
  115. display: none;
  116. }
  117. </style>