固定表頭和第一列、內容可滾動的table表格

html:

<div class="tab">

    <div class="container">
        <div id="left-div">
            <div id="left-div1">
                <table>
                    <tr>
                        <th>編號</th>
                    </tr>
                </table>
            </div>
            <div id="left-div2">
                <table id="left-table2"></table>
            </div>
        </div>
        <div id="right-div">
            <div id="right-div1">
                <table id="right-table1">
                    <tr>
                        <th>設備名稱</th>
                        <th>設備類型</th>
                        <th>故障類型</th>
                        <th>故障狀態</th>
                    </tr>
                </table>
            </div>
            <div id="right-div2">
                <table id="right-table2"></table>
            </div>
        </div>
    </div>


</div>

css:

<style>
    *{
        margin:0;
        padding: 0;
    }
    table{
        width:100%;
        text-align:center;
        border-collapse:collapse;
        border-spacing:0;
    }
    table td{
        word-break: break-all;
        word-wrap:break-word;
    }
    .container{
        width: 600px;
        height: 500px;
        padding: 0;
        box-sizing: border-box;
    }
    #left-div{
        width:80px;
        float: left;
    }
    #left-div1{
        width: 100%;
    }
    #left-div2{
        width: 100%;
        height: 250px;
        overflow: hidden;
    }
    #left-table2{
        margin-bottom: 4px;
    }
    #right-div{
        float: left;
        width: 240px;
    }
    #right-div1{
        width: 100%;
        overflow: hidden;
    }
    #right-div2{
        width: 100%;
        height: 250px;
        overflow: auto;
    }
    #right-table1{
        width: 320px;
    }
    #right-table2{
        width: 320px;
        overflow: auto;
    }
    th,td{
        height: 50px;
        width: 80px;
        line-height: 50px;
        overflow: hidden;
        text-align: center;
    }
    th{
        color: #1E304F;
        background-color: #F3F8FF;
    }
    td{
        color: #384967;
    }
    tr:nth-of-type(odd){
        background: #E7F2FF;
    }
    /*可以美化一下滾動條*/
    #right-div2::-webkit-scrollbar {/*滾動條整體樣式*/
        width: 4px;
        height: 4px;
    }
    #right-div2::-webkit-scrollbar-thumb {/*滾動條裏面小方塊*/
        border-radius: 5px;
        box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
        background: rgba(0,0,0,0.2);
    }
    #right-div2::-webkit-scrollbar-track {/*滾動條裏面軌道*/
        box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
        border-radius: 0;
        background: rgba(0,0,0,0.1);
    }

</style>

js:

<script>
    $(function () {
        //生成表格內容
        let htmlLeft = '';
        let htmlRight = '';
        for(let i=1;i<=7;i++){
            htmlLeft +='<tr>';
            htmlLeft +='<td>'+i+'</td>';
            htmlLeft +='</tr>';
        }
        for(let i=1;i<=7;i++){
            htmlRight+='<tr>';
            htmlRight+='<td>A</td>';
            htmlRight+='<td>100</td>';
            htmlRight+='<td>500</td>';
            htmlRight+='<td>1</td>';
            htmlRight+='</tr>';
        }
        $('#left-table2').html(htmlLeft);
        $('#right-table2').html(htmlRight);
        //滾動
        $('#right-div2').on('scroll',function(){
            let top=$(this).scrollTop();
            let left=$(this).scrollLeft();
            $('#left-div2').scrollTop(top);
            $('#right-div1').scrollLeft(left);
        })

    });
</script>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章