CSS—table—固定列和行

11 篇文章 0 订阅
  1. 给table表格设置固定布局
table {
	table-layout: fixed // 表格的总宽度决定于表格width的定义,以及各栏位(column)width的定义
}
  1. 设置单元格th 或 td 固定定位
.main .sticky {
    position: sticky;  // 表现类似于relative 和fixed 的合体
     z-index: 1; // 让固定层比滑动层的层级高一级
     background-color: lightpink; // 用于滑动时,挡住滑动层的内容
 }
  1. 设置单元格(th、td)到table左上角的距离
:style="index < 4 ? {'left':width*index + 'px'} : {}"
:style="index < 2 ? {'top':height*(index+1) + 'px'} : {}"
  1. 滚动时,固定部分只包括td或、th的 宽度和高度的内容, 不包括 boder 边框,所以滚动时,边框无法一起滚动。
    解决方案分两步:1. 给td增加1px的高度;2.使用before或者after伪类,模拟边框并定位,代码如下
td
   position: sticky;
   z-index: 1;
   background-color: #fff;
   height:31px
   &::before
    position: absolute;
    content: '';
    left: 0 ;
    bottom: 0;
    width:100%
    height:1px 
    background:#bbcae7;

把案例代码到复制到本地,使用浏览器看效果,如果给你开发或者学习带来帮助,记得一键三连,谢谢大家鼓励和支持。
6. 案例代码

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>纯CSS实现表格首行和首列固定</title>
    <!-- 插入Vue -->
    <!-- PS: 如果页面加载不出来,请检查vue引入是否正常,如无法打开,请到前端CDN库查找对应的资源引入,推荐网址 https://blog.csdn.net/muzihuaner/article/details/122264730 -->
    
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.4/vue.common.dev.js"></script>
    <style>
        .main {
            width: 1000px;
            overflow: auto;
            height: 208px;
            /* 设置固定高度 */
        }
        td,
        th {
            /* 设置td,th宽度高度 */
            border: 1px solid gray;
            width: 100px;
            height: 30px;
        }

        th {
            background-color: lightblue;
        }

        table {
            table-layout: fixed;
            width: 100%;
            /* 固定宽度 */
        }

        .main .sticky {
            position: sticky;
            z-index: 1;
            background-color: lightpink;
        }
    </style>
</head>

<body>
    <div id="app">
    	<!-- 列固定案例 -->
        <div class="main">
            <table cellspacing="0">
                <thead>
                    <tr>
                        <!-- <th v-for="(item, index) in 20" :class="{'sticky' : index < 4 }" :style="leftStyle(index)">头部测试{{item}}</th> -->
                        <th v-for="(item, index) in 20"
                         :key="index" 
                         :class="{'sticky' : index < 4 }"
                         :style="index < 4 ? {'left':width*index + 'px'} : {}">
                         头部测试{{item}}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in 4" :key="index">
                        <!-- 判断较为复杂的逻辑建议用计算函数 -->
                        <td v-for="(item1,index1) in 20"
                         :key="index"
                         :class="{'sticky' : index1 < 4 }"
                         :style="leftStyle(index1)">
                         测试{{item1}}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
		<!-- 行固定案例 -->
        <div class="main">
            <table cellspacing="0">
                <thead>
                    <tr>
                        <!-- <th v-for="(item, index) in 20" :class="{'sticky' : index < 4 }" :style="leftStyle(index)">头部测试{{item}}</th> -->
                        <th v-for="(item, index) in 20"
	                       :key="index"
	                       class="sticky"
	                       style="top: 0px;">
	                       头部测试{{item}}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in 20" :key="index">
                        <!-- 判断较为复杂的逻辑建议用计算函数 -->
                        <td
                         v-for="(item1,index1) in 20"
                         :key="index"
                         :class="{'sticky' : index < 2 }"
                         :style="index < 2 ? {'top':height*(index+1) + 'px'} : {}">
                         测试{{item1}}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            message: 'Hello',
            width: 104,
            height: 34
        },
        computed: {
            leftStyle() {
                return function (index) {
                    return index < 4 ? {
                        left: this.width * index + 'px'
                    } : {}
                }
            }
        }
    })
</script>

</html>

三、参考文章
table固定行列

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帝博格T-bag

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值