news 2026/6/10 15:05:00

前端table表格,零基础入门到精通,收藏这篇就够了

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
前端table表格,零基础入门到精通,收藏这篇就够了

1.写一个简单的table表格框架

<table> <thead class="my-table"> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.address }}</td> </tr> </tbody> </table>

table里的数据在data里返回,以下是实例数据

tableData: [ { name: "张三", age: 30, address: "北京市朝阳区" }, { name: "李四", age: 28, address: "上海市浦东新区" }, { name: "王五", age: 32, address: "广州市天河区" }, { name: "赵六", age: 25, address: "深圳市福田区" }, { name: "孙七", age: 27, address: "成都市武侯区" }, ],

这样我们就拥有了一个没有样式的基础表格

2.为了使表格更好看,我实现了如下几步:

  1. 固定表头,滚动时保留表格上面的标题

将th的 position设置为sticky,固定首行,也就是top=0,为其设置为一个区分的颜色

th { background-color: #3c7ca7; font-weight: bold; position: sticky; top: 0; background-color: rgb(110, 138, 163); }
  1. 将表格间的分割线去除
<table cellspacing="0" > <--...table内容--> </table>
  1. 修改表格背景颜色,字体等样式
    为了好看点还设置了隔行的颜色区分也就是 tr:nth-child(even)
th, td { padding: 20px; text-align: center; font-family: '微软雅黑',"times","courier","arial"; font-weight:"medium"; //border: 1px solid #ccc; font-size: 20px; color: #ffffff; } th { background-color: #3c7ca7; font-weight: bold; position: sticky; top: 0; background-color: rgb(110, 138, 163); } tr:nth-child(even) { background-color: #3c7ca7; }
  1. 自动滚动效果
    首先为了滚动效果的顺利实现,给table类增加一个外框类table-container
    增加滑动框并隐藏
.table-container { overflow-y: scroll; -webkit-overflow-scrolling: touch; } .table-container::-webkit-scrollbar { display: none; }

然后利用timer定时器和scrollTop 属性设置滚动效果,

mounted() { this.timerId = setInterval(() => { const tableContainer = this.$refs.tableContainer; const rowHeight = tableContainer.querySelector('tbody tr').offsetHeight; tableContainer.scrollTop += rowHeight; if (tableContainer.scrollTop == lasttop) { tableContainer.scrollTop = 0; }else{ lasttop=tableContainer.scrollTop } }, 2000); this.init(); }, beforeDestroy() { // 在组件销毁前清除 setInterval clearInterval(this.timerId);

},
由于上面的滚动效果不好,看起来很卡顿,下面优化了滚动效果,加入缓动

mounted() { this.timerId = setInterval(() => { const tableContainer = this.$refs.tableContainer; const rowHeight = tableContainer.querySelector('tbody tr').offsetHeight; tableContainer.scrollTop += rowHeight/30; if (tableContainer.scrollTop == lasttop) { tableContainer.scrollTop = 0; }else{ lasttop=tableContainer.scrollTop } }, 30); this.init(); // this.tableroll(); },
  1. 最后实现效果

3.全部代码

<div class="table-container" ref="tableContainer"> <table cellspacing="0" > <thead class="my-table"> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.address }}</td> </tr> </tbody> </table> </div> export default { data() { tableData: [ { name: "张三", age: 30, address: "北京市朝阳区" }, { name: "李四", age: 28, address: "上海市浦东新区" }, { name: "王五", age: 32, address: "广州市天河区.dfasfaweorfaiadsfasd" }, { name: "赵六", age: 25, address: "深圳市福田区" }, { name: "孙七", age: 27, address: "成都市武侯区" }, ], }, .table-container::-webkit-scrollbar { display: none; } .table-container { position: absolute; left: 80%; top:70%; height: 300px; overflow-y: scroll; -webkit-overflow-scrolling: touch; background-color:rgba(18, 76, 117, 0.7); .my-table { } th, td { padding: 20px; text-align: center; font-family: '微软雅黑',"times","courier","arial"; font-weight:"medium"; //border: 1px solid #ccc; font-size: 20px; color: #ffffff; } th { background-color: #3c7ca7; font-weight: bold; position: sticky; top: 0; background-color: rgb(110, 138, 163); } tr:nth-child(even) { background-color: #3c7ca7; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/9 18:40:12

为什么顶尖团队都在用Open-AutoGLM做任务调度?真相令人震惊

第一章&#xff1a;为什么顶尖团队都在用Open-AutoGLM做任务调度&#xff1f;真相令人震惊在现代分布式系统中&#xff0c;任务调度的效率直接决定了系统的吞吐与稳定性。Open-AutoGLM 作为一款开源的智能任务调度引擎&#xff0c;正被 Google、Meta 和阿里云等顶尖技术团队秘密…

作者头像 李华
网站建设 2026/6/10 1:43:58

vue3+springboot美妆店化妆品商城系统 美容院预约系统 uniapp微信小程序设计与实现(编号:06310976)

文章目录具体实现截图主要技术与实现手段关于我本系统开发思路java类核心代码部分展示结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;具体实现截图 同行可拿货,招校园代理 vue3springboot美妆店化妆品商城系统 美容院预约系统 uni…

作者头像 李华
网站建设 2026/6/10 12:59:59

告别慢速匹配:Open-AutoGLM模式引擎优化的7个关键步骤

第一章&#xff1a;告别慢速匹配——Open-AutoGLM模式引擎优化的必要性在大规模语言模型应用场景中&#xff0c;传统正则匹配与规则驱动的文本处理方式已难以满足实时性与准确性的双重需求。面对海量非结构化数据的快速解析任务&#xff0c;系统响应延迟显著上升&#xff0c;严…

作者头像 李华
网站建设 2026/6/9 15:10:46

为什么顶尖团队都在用Open-AutoGLM做推理优化?真相令人震惊

第一章&#xff1a;为什么顶尖团队都在用Open-AutoGLM做推理优化&#xff1f;真相令人震惊在大模型推理效率成为核心瓶颈的今天&#xff0c;Open-AutoGLM凭借其革命性的自适应图优化引擎&#xff0c;正在被头部AI实验室和科技巨头悄然部署。它不仅能自动识别并压缩冗余计算图节…

作者头像 李华
网站建设 2026/6/10 12:59:00

惊!中科院1区Top权威顶刊降为2区

&#x1f525; &#x1f525; &#x1f525; &#x1f525;《Computers & Industrial Engineering》创刊于1976年&#xff0c;是工业工程与计算机科学交叉领域中历史悠久、享有盛誉的国际顶尖期刊&#xff0c;以其扎实的学术质量和明确的应用导向著称。它不仅见证了该…

作者头像 李华