固定表头(利用CSS3)

<!DOCTYPE html>
<html>


<head>
    <link data-require="bootstrap@*" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <style>
        *{ margin: 0; padding: 0; list-style: none;}
        .box {
            width: 300px;
            height: 300px;
            margin: 50px auto 0;
            overflow: auto;
        }
        .box table{
            width: 100%;
            border-collapse: collapse;
            border-right: 1px solid #ccc;
            border-top: 1px solid #ccc;
            text-align: center;
        }
        .box table thead {
            background-color: #ccc;
        }
        .box table th,
        .box table td {
            padding: 8px 10px;
            border-left: 1px solid #ccc;
            border-bottom: 1px solid #ccc;
            white-space: nowrap;
        }
    </style>
</head>


<body>
<div class="box">
    <table>
        <thead>
        <tr>
            <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
            <th>6</th>
            <th>7</th>
            <th>8</th>
            <th>9</th>
            <th>10</th>
            <th>11</th>
            <th>12</th>
            <th>13</th>
            <th>14</th>
            <th>15</th>
        </tr>
        </thead>
        <tbody>
        <script>
            var tr = '';
            for(var i=0; i<15; i++) {
                tr += '<tr>\
                        <td>'+i+'<span>哈哈哈哈哈哈哈哈</span></td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                        <td>'+i+'</td>\
                    </tr>';
            }
            document.write(tr);
        </script>
        </tbody>
    </table>
</div>
<script>
    window.onload = function() {
        var $ = document.querySelector.bind(document);
        var boxEle = $('.box');
        boxEle.addEventListener('scroll', function(e) {
            this.querySelector('thead').style.transform = 'translate(0, '+this.scrollTop+'px)';
        });
    }
</script>
</body>


</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值