原生css table固定高度+固定表头

转载https://www.cnblogs.com/nanfei/p/16319915.html

略有修改


    .table-fixedHeader {
        max-height: 60vh;
        overflow-y: auto;
        overflow-x: hidden;
        display: block;
        width: 100%;
        table-layout: fixed;
        --vetialBorder: 1px;
    }
    
    .table-fixedHeader thead {
        border-left: 1px solid #DFE2EB;
        border-top: 1px solid #DFE2EB;
        position: sticky;
        top: 0;
        left: 0;
        right: 0;
        display: table;
        width: 100%;
        table-layout: fixed;
    }
    
    .table-fixedHeader thead tr {
        height: 40px;
        line-height: 40px;
    }
    
    .table-fixedHeader thead tr>th {
        background-color: #fafafa;
        font-weight: bold;
        border-bottom: 1px solid #DFE2EB;
        border-right: var(--vetialBorder) solid #DFE2EB;
        color: #909399;
        text-align: center;
        min-width: 40px;
    }
    
    .table-fixedHeader tbody {
        border-left: 1px solid #DFE2EB;
        display: table;
        width: 100%;
        table-layout: fixed;
    }
    
    .table-fixedHeader tbody tr {
        height: 40px;
        line-height: 40px;
    }
    
    .table-fixedHeader tbody tr td {
        border-bottom: 1px solid #DFE2EB;
        border-right: var(--vetialBorder) solid #DFE2EB;
        text-align: center;
    }

        <div class="container-fluid  goods_table table-responsive">
            <table class="table table-hover table-fixedHeader">
                <thead>
                    <tr>
                        <th class="hidden-xs" width="36px">
                            <input type="checkbox" id="checkAll" />
                        </th>
                        <th width="100px">序号</th>
                        <th width="300px">商品名</th>
                        <th>分类</th>
                        <th>状态</th>
                        <th>规格</th>
                        <th width="120px">数量</th>
                        <th>价格</th>
                        <th>过期日期</th>
                        <th>sku</th>
                        <th>69码</th>
                    </tr>
                </thead>
                <tbody>
                        <tr>
                            <td class="hidden-xs" width="36px">
                            	<input type="checkbox" name="chkItem" value="9865" />
                            </td>
                            <td width="100px">9865</td>
                            <td width="300px">买不起卖不起买不起卖不起56555ml超级热销产品</td>
                            <td>无敌分类</td>
                            <td style="color: red;">下架</td>
                            <td>超级袋装</td>
                            <td width="120px">665</td>
                            <td>¥999.99</td>
                            <td>2323-02-10 15:11</td>
                            <td>65889944112235555</td>
                            <td>695789454115</td>
                        </tr>
                </tbody>
            </table>
        </div>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue原生table表格表头固定,可以通过CSS的position属性和JS的scroll事件来实现。 首先,在table标签外面嵌套一个div容器,设置其样式为position: relative,用于容纳表格和表头。 然后,在表格表头的tr标签上添加一个ref属性,用于在JS中获取该元素。 接下来,使用JS监听div容器的scroll事件,在事件中通过ref获取到表头元素,并获取该元素的offsetTop和scrollTop属性值。 然后,判断scrollTop是否大于或等于offsetTop,如果是则添加一个css类或样式,将表头固定在顶部;如果不是,则移除该类或样式。 最后,将改变样式的操作放在一个debounce的函数中,用于优化滚动事件的性能。 具体代码如下: <template> <div class="container" ref="container" @scroll="handleScroll"> <table class="table"> <thead> <tr ref="thead"> <th>表头1</th> <th>表头2</th> <th>表头3</th> </tr> </thead> <tbody> <tr> <td>内容1</td> <td>内容2</td> <td>内容3</td> </tr> ... </tbody> </table> </div> </template> <script> export default { methods: { handleScroll() { const container = this.$refs.container; const thead = this.$refs.thead; const offsetTop = thead.offsetTop; const scrollTop = container.scrollTop; if (scrollTop >= offsetTop) { thead.classList.add("fixed"); } else { thead.classList.remove("fixed"); } }, }, }; </script> <style> .container { position: relative; max-height: 300px; overflow-y: scroll; } .fixed { position: fixed; top: 0; left: 0; width: 100%; z-index: 999; } .table { width: 100%; } /* 省略其他样式 */ </style> 以上就是利用Vue原生实现table表格表头固定的方法。通过设置CSS样式和监听scroll事件,可以在滚动时使表头保持在页面顶部。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值