table表头和首列的表格固定-CSS实现的Table表头固定

这里写图片描述

效果就是上图 表头是固定的, 
跟随滑动轴动,左边第一列也是可以跟谁滑动轴滚动 
下面是代码原文是查看

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="author" content="jack">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,
        user-scalable=no" />
<!-- 适应手机屏幕,防止屏幕缩放 -->

<!--样式-->
<link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/bootstrap-3.3.4.css">
</head>

<body>
<title>固定表头和首列的表格</title>
<style type="text/css">
#left_div{
    width:100px;
    float: left;
}
#left_div1{
    width: 100%;
}
#left_div2{
    margin-top:-20px;
    width: 100%;
    height: 400px;
    overflow: hidden;
}
#left_table1 th{
    background: #E9F8FF;
    text-align:center;
}
#left_table2 th{
    text-align:center;
}

#right_div{
    float: left;
}
#right_div1{
    width: 100%;
    overflow: hidden;
}
#right_divx{
    width: 900px;
}
#right_div2{
    margin-top:-20px;
    width:100%;
    height:400px;
    overflow: auto;
}
#right_table1{
    width: 880px;
}
#right_table2{
    /**width和max-width一起写,手机浏览器打开也能固定长度**/
    width: 880px;
    max-width: 880px;
    white-space:nowrap;
}
#right_table1 th{
    background: #E9F8FF;
    text-align:center;
    width:10%;
}
#right_table2 td{
    width:10%;
    text-align:center;
}

</style>
<div class="container-fluid">
  <div id="left_div">
    <div id="left_div1">
      <table id="left_table1" class="table table-bordered">
        <tr>
          <th>我不会动</th>
        </tr>
      </table>
    </div>
    <div id="left_div2">
      <table id="left_table2" class="table table-bordered">
      </table>
    </div>
  </div>
  <div id="right_div">
    <div id="right_div1">
      <div id="right_divx">
        <table id="right_table1" class="table table-bordered">
          <tr>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
            <th>我是表头</th>
          </tr>
        </table>
      </div>
    </div>
    <div id="right_div2">
      <table id="right_table2" class="table table-bordered">
      </table>
    </div>
  </div>
</div>

<!--脚本--> 
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> 
<script src="./bootstrap.min.js" type="text/javascript"></script> 
<script type="text/javascript">

//固定和滚动
var right_div2 = document.getElementById("right_div2");
right_div2.onscroll = function(){
    var right_div2_top = this.scrollTop;
    var right_div2_left = this.scrollLeft;
    document.getElementById("left_div2").scrollTop = right_div2_top;
    document.getElementById("right_div1").scrollLeft = right_div2_left;
}
//设置右边div宽度
document.getElementById("right_div").style.width=""+document.documentElement.clientWidth-130+"px";  
setInterval(function() {
    document.getElementById("right_div").style.width=""+document.documentElement.clientWidth-130+"px";  
}, 0);

for(var i=0;i<24;i++){
    $("#left_table2").append("<tr><th>我是首列</th></tr>");
    $("#right_table2").append("<tr><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>");
}

</script>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141

这句js 不要忘记加了

var right_div2 = document.getElementById("right_div2");
right_div2.onscroll = function(){
    var right_div2_top = this.scrollTop;
    var right_div2_left = this.scrollLeft;
    document.getElementById("left_div2").scrollTop = right_div2_top;
    document.getElementById("right_div1").scrollLeft = right_div2_left;
}
//设置右边div宽度
document.getElementById("right_div").style.width=""+document.documentElement.clientWidth-130+"px";  
setInterval(function() {
    document.getElementById("right_div").style.width=""+document.documentElement.clientWidth-130+"px";  
}, 0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

如果你遇到问题,可以加群315552185 
一起交流哦!

转自:http://blog.csdn.net/q3585914/article/details/69946478

猜你喜欢

转载自blog.csdn.net/u014538198/article/details/78190851