Vue多种方法实现表头、首列固定

有时表格太大,滚动时信息查看不方便,需要对表格进行全局表头、首列固定,
上效果:
固定表头首列

一、创建多个表格进行覆盖

    思路:当页面滚动到临界值时,出现固定表头、首列
    先创建一个活动表格
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            th,
            td {
                min-width: 200px;
                height: 50px;
            }
            #sTable {
                margin-top: 300px }
            [v-cloak]{
                display: none;
            }
        </style>
    </head>
    <body v-cloak>
        <!--活动的表格-->
        <table id="sTable" border="1" cellspacing="0">
            <thead>
                <tr>
                    <th v-for="item in th">{
   {
   item.key}}</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in tl">
                    <td  v-for="list in item">{
   {
   list.key}}</td>
                </tr>
            </tbody>
        </table>
        <script src="vue.js"></script>
        <script src="jquery.js"></script>
        <script>
            var vm = new Vue({
                el: "body",
                data: function() {
     
                    return {
                        th: [],
                        tl: [],
                        temp: [],
                    }
                },
                methods: {
                    //生成表格
                    CTable: function() {
     
                        for(var i = 0; i < 10; i++) {
                            this.th.push({
                                key: "head" + i
                            })
                        }
                        for(var i = 0; i < 100; i++) {
                            for(var j = 0; j < 10; j++) {
                                this.temp.push({
                                    key: j + "body" + i
                                })
                            }
                            this.tl.push(this.temp)
                            this.temp = []
                        }
                    },
                },
                ready: function() {
     
                    this.CTable();
                },
            })
        </script>
    </body>
</html>

再添加固定表头:

#fHeader {
    background: lightblue;
    position: fixed;
    top: 0;
}
<!--固定表头-->
<table border="1" id="fHeader" cellspacing="0" v-show="fixedHeader"> 
    <thead>
        <tr >
            <th v-for="item in th">{
   {
   item.key}}</th>
        </tr>
    </thead>
</table>

监控表格位置到达窗口顶部时出现固定表头

//监控表头位置
headerMonitor:function(){
   
    var self=this
    var hHeight=$("#sTable").offset().top;
    $(document).scroll(function(){
   
        //当滚动条达到偏移值的时候,出现固定表头
        <
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值