1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| <template> <div class="table-box"> <div class="table-th"> <div style="width: 60px">排名</div> <div>区域</div> <div>总服务人数</div> <div>2023年</div> <div>2024年</div> </div> <div class="table-tr" ref="tableTr" @mouseenter="pause" @mouseleave="resume"> <div class="tr-item" v-for="(item, index) in [...tableData, ...tableData]" :key="index" :class="{ 'even-background': index < 15 ? index % 2 !== 0 : index % 2 === 0 }" > <div class="index-item"> <div v-if="item.ranking <= 3" class="tab-index index-bg"> {{ item.ranking }} </div> <div v-else class="tab-index"> {{ item.ranking }} </div> </div> <div>{{ item.province }}</div> <div>{{ item.totalServiceNumber }}</div> <div>{{ item.yearNumber2 }}</div> <div>{{ item.yearNumber3 }}</div> </div> </div> </div> </template>
<script> export default { name: 'demo2', data() { return { tableData: [], position: 0, animationId: null, } }, mounted() { this.getTableData() this.$nextTick(() => { this.animation() }) }, methods: { /** * 暂停动画。 * 此方法通过取消动画帧请求来暂停动画的执行。 * 当鼠标移入组件时调用,以暂停动画的播放。 */ pause() { cancelAnimationFrame(this.animationId) this.animationId = null this.isPaused = true }, /** * 恢复动画。 * 如果动画当前处于暂停状态,此方法会重新启动动画。 * 当鼠标移出组件时调用,以恢复动画的播放。 */ resume() { if (this.isPaused) { this.animation() this.isPaused = false } }, /** * 启动一个动画,用于滚动表格中的行。 * 此函数没有参数和返回值,因为它直接操作DOM并启动一个循环动画。 * 动画效果是通过改变行的垂直偏移来实现的。 */ animation() { let tableTr = this.$refs.tableTr let tableTrHeight = tableTr.offsetHeight /** * 动画循环函数。 * 不断调整元素的垂直偏移,创造向下滚动的效果。 * 当元素偏移超过其高度的一半时,重置偏移值,实现循环滚动。 */ const animate = () => { this.position -= 2 if (this.position <= -(tableTrHeight / 2)) { this.position = 0 } tableTr.style.transform = `translateY(${this.position}px)` this.animationId = requestAnimationFrame(animate) } animate() }, getTableData() { const generatedData = [] const provinces = [ '北京', '上海', '广东', '江苏', '浙江', '山东', '河南', '湖南', '湖北', '四川', '福建', '安徽', '陕西', '重庆', '云南', ] const randomServiceNumber = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min for (let i = 0; i < 15; i++) { const province = provinces[Math.floor(Math.random() * provinces.length)] const totalServiceNumber = randomServiceNumber(1000, 10000).toString() const yearNumber2 = randomServiceNumber(500, 5000).toString() const yearNumber3 = randomServiceNumber(1000, 8000).toString()
generatedData.push({ ranking: i + 1, province, totalServiceNumber, yearNumber2, yearNumber3, }) }
this.tableData = generatedData }, }, } </script>
<style scoped lang="scss"> .table-box { background-color: #000; color: white; / / 最外层父元素必须要定高 width: 500 px; height: 385px; overflow: hidden; }
.table-th { display: flex; align-items: center; justify-content: space-around; background-color: #1a8dec; font-weight: 100; font-size: 14px; padding: 2px 0; height: 35px; line-height: 35px; position: sticky; z-index: 2;
& > div { width: 120px; text-align: center; }
}
.table-tr { display: flex; flex-direction: column;
.tr-item { display: flex; align-items: center; justify-content: space-around; font-weight: 100; font-size: 12px; height: 35px; / / 每一行的高度也必须固定,用户计算滚动距离 line-height: 35 px;
& > div { width: 120px; text-align: center; }
.index-item { width: 60px; display: flex; align-items: center; justify-content: center; }
}
/ / .tr-item:nth-child(odd) { / / background-color: #322c5f; / / }
}
.even-background { background-color: #322c5f; }
.tab-index { width: 16px; height: 16px; line-height: 16px;
text-align: center; border-radius: 50%; background-color: #4666ff; }
.index-bg { background-color: #f43737; } </style>
|