html中固定table的首行和首列

见代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>
        body {
            background: #FFFFFF; }
        body .table_div_wrap {
            border: 1px red solid;
            background: #f0f0f0;
            width: 80%;
            min-width: 320px;
            max-width: 600px;
            margin: 0 auto;
            margin-top: 100px;
            position: relative;
            overflow: hidden; }
        body .table_div_wrap .inner .table_tbody {
            width: 100%;
            height: 300px;
            overflow-y: auto;
            overflow-x: auto; }
        body .table_div_wrap .inner table {
            min-width: 100%; }
        body .table_div_wrap .inner table td, body .table_div_wrap .inner table th {
            border-top: 1px black solid;
            border-right: 1px black solid;
            min-width: 150px;
            height: 80px; }
        body .table_div_wrap .inner table th {
            border: none;
            border-right: 1px black solid;
            height: 30px; }
        body .table_div_wrap .outer {
            position: absolute;
            top: 0;
            left: 0; }
        body .table_div_wrap .outer .table_tbody {
            width: 100%;
            height: 300px;
            overflow-y: hidden;
            overflow-x: scroll; }
        body .table_div_wrap .outer table {
            min-width: 100%; }
        body .table_div_wrap .outer table td, body .table_div_wrap .outer table th {
            border-top: 1px black solid;
            border-right: 1px black solid;
            min-width: 150px;
            height: 80px;
            display: none; }
        body .table_div_wrap .outer table th {
            border: none;
            border-right: 1px black solid;
            height: 30px; }
        body .table_div_wrap .outer table th:first-child {
            display: table-cell;
            background: #cccccc; }
        body .table_div_wrap .outer table td:first-child {
            display: table-cell;
            background: #cccccc; }

    </style>
</head>
<body>
<div class="table_div_wrap">
    <div class="inner">
        <div class="table_thead">
            <table cellpadding="0" cellspacing="0">
                <thead>
                <tr>
                    <th>first</th>
                    <th>second</th>
                    <th>third</th>
                    <th>fourth</th>
                    <th>fifth</th>
                </tr>
                </thead>
            </table>
        </div>
        <div class="table_tbody">
            <table cellpadding="0" cellspacing="0">
                <tbody>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="outer">
        <div class="table_thead">
            <table cellpadding="0" cellspacing="0">
                <thead>
                <tr>
                    <th>first</th>
                    <th>second</th>
                    <th>third</th>
                    <th>fourth</th>
                    <th>fifth</th>
                </tr>
                </thead>
            </table>
        </div>
        <div class="table_tbody">
            <table cellpadding="0" cellspacing="0">
                <tbody>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
<script>
    var table_tbody = document.getElementsByClassName("table_tbody")[0];
    var table_thead = document.getElementsByClassName("table_thead")[0];
    var table_tbody_outer = document.getElementsByClassName("table_tbody")[1].getElementsByTagName('table')[0];
    table_tbody.onscroll = function (e) {
        // console.log(this.scrollLeft,this.scrollTop)
        // console.log(table_tbody_outer)
        table_thead.style.marginLeft = "-"+this.scrollLeft+"px"
        table_tbody_outer.style.marginTop = "-"+this.scrollTop+'px'
    }
</script>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值