<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>new document</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
function FixTable(TableID, FixColumnNumber, width, height) {
if ($("#" + TableID + "_tableLayout").length != 0) {
$("#" + TableID + "_tableLayout").before($("#" + TableID));
$("#" + TableID + "_tableLayout").empty();
}
else {
$("#" + TableID).after("<div id='" + TableID + "_tableLayout' style='overflow:hidden;height:" + height + "px; width:" + width + "px;'></div>");
}
$('<div id="' + TableID + '_tableFix"></div>'
+ '<div id="' + TableID + '_tableHead"></div>'
+ '<div id="' + TableID + '_tableColumn"></div>'
+ '<div id="' + TableID + '_tableData"></div>').appendTo("#" + TableID + "_tableLayout");
var oldtable = $("#" + TableID);
var tableFixClone = oldtable.clone(true);
tableFixClone.attr("id", TableID + "_tableFixClone");
$("#" + TableID + "_tableFix").append(tableFixClone);
var tableHeadClone = oldtable.clone(true);
tableHeadClone.attr("id", TableID + "_tableHeadClone");
$("#" + TableID + "_tableHead").append(tableHeadClone);
var tableColumnClone = oldtable.clone(true);
tableColumnClone.attr("id", TableID + "_tableColumnClone");
$("#" + TableID + "_tableColumn").append(tableColumnClone);
$("#" + TableID + "_tableData").append(oldtable);
$("#" + TableID + "_tableLayout table").each(function () {
$(this).css("margin", "0");
});
var HeadHeight = $("#" + TableID + "_tableHead thead").height();
HeadHeight += 2;
$("#" + TableID + "_tableHead").css("height", HeadHeight);
$("#" + TableID + "_tableFix").css("height", HeadHeight);
var ColumnsWidth = 0;
var ColumnsNumber = 0;
$("#" + TableID + "_tableColumn tr:last td:lt(" + FixColumnNumber + ")").each(function () {
ColumnsWidth += $(this).outerWidth(true);
ColumnsNumber++;
});
ColumnsWidth += 2;
if ($.browser.msie) {
switch ($.browser.version) {
case "7.0":
if (ColumnsNumber >= 3) ColumnsWidth--;
break;
case "8.0":
if (ColumnsNumber >= 2) ColumnsWidth--;
break;
}
}
$("#" + TableID + "_tableColumn").css("width", ColumnsWidth);
$("#" + TableID + "_tableFix").css("width", ColumnsWidth);
$("#" + TableID + "_tableData").scroll(function () {
$("#" + TableID + "_tableHead").scrollLeft($("#" + TableID + "_tableData").scrollLeft());
$("#" + TableID + "_tableColumn").scrollTop($("#" + TableID + "_tableData").scrollTop());
});
$("#" + TableID + "_tableFix").css({ "overflow": "hidden", "position": "relative", "z-index": "50", "background-color": "Silver" });
$("#" + TableID + "_tableHead").css({ "overflow": "hidden", "width": width - 17, "position": "relative", "z-index": "45", "background-color": "Silver" });
$("#" + TableID + "_tableColumn").css({ "overflow": "hidden", "height": height - 17, "position": "relative", "z-index": "40", "background-color": "Silver" });
$("#" + TableID + "_tableData").css({ "overflow": "scroll", "width": width, "height": height, "position": "relative", "z-index": "35" });
if ($("#" + TableID + "_tableHead").width() > $("#" + TableID + "_tableFix table").width()) {
$("#" + TableID + "_tableHead").css("width", $("#" + TableID + "_tableFix table").width());
$("#" + TableID + "_tableData").css("width", $("#" + TableID + "_tableFix table").width() + 17);
}
if ($("#" + TableID + "_tableColumn").height() > $("#" + TableID + "_tableColumn table").height()) {
$("#" + TableID + "_tableColumn").css("height", $("#" + TableID + "_tableColumn table").height());
$("#" + TableID + "_tableData").css("height", $("#" + TableID + "_tableColumn table").height() + 17);
}
$("#" + TableID + "_tableFix").offset($("#" + TableID + "_tableLayout").offset());
$("#" + TableID + "_tableHead").offset($("#" + TableID + "_tableLayout").offset());
$("#" + TableID + "_tableColumn").offset($("#" + TableID + "_tableLayout").offset());
$("#" + TableID + "_tableData").offset($("#" + TableID + "_tableLayout").offset());
}
$(document).ready(function () {
FixTable("MyTable", 2, 800, 400);
});
//-->
</script>
</head>
<body>
<table
style="border-bottom-color: black; border-top-color: black; width: 1000px; color: #000000; border-right-color: black; font-size: medium; border-left- color: black"
id="MyTable" border="1" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="text-align: center; width: 80px" >姓名</th>
<th style="text-align: center; width: 80px" >班级</th>
<th style="text-align: center; width: 80px">语文</th>
<th style="text-align: center; width: 80px">数学</th>
<th style="text-align: center; width: 80px">英语</th>
<th style="text-align: center; width: 80px">政治</th>
<th style="text-align: center; width: 80px">历史</th>
<th style="text-align: center; width: 80px">地理</th>
<th style="text-align: center; width: 80px">物理</th>
<th style="text-align: center; width: 80px">化学</th>
<th style="text-align: center; width: 80px">生物</th>
<th style="text-align: center; width: 80px">生物</th>
</tr>
</thead>
<tbody>
<!-- 数据行 -->
<tr>
<td>学生3asfasfdasfsdaf2</td>
<td>班级1</td>
<td>29safafafasfasdf</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
<tr>
<td>学生32</td>
<td>班级1</td>
<td>29</td>
<td>25</td>
<td>146</td>
<td>28</td>
<td>79</td>
<td>73</td>
<td>47</td>
<td>8</td>
<td>91</td>
<td>526</td>
</tr>
</tbody>
</table>
</body>
</html>
<!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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题页</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
body{font-size:12px;}
table{border:solid 1px #ccc; border-collapse:collapse;}
table td,table th{border:solid 1px #ccc; line-height:22px; height:22px; padding:0;}
#tbheader td{background:#fff;}
</style>
<script type="text/javascript" >
var ofixed_table_st = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
if (typeof fRef == 'function') {
var argu = Array.prototype.slice.call(arguments, 2);
var f = (function() {
fRef.apply(null, argu);
});
return ofixed_table_st(f, mDelay);
}
return ofixed_table_st(fRef, mDelay);
};
function oFixedTable(id, obj, _cfg){
this.id = id;
this.obj = obj;
this.box = this.obj.parentNode;
this.config = {
fixHead: _cfg.fixHead || true,
rows: _cfg.rows || 1,
cols: _cfg.cols || 0,
background: _cfg.background || '#f1f1f1',
zindex: _cfg.zindex || 9999
};
window.setTimeout(this._fixTable, 100, this);
}
oFixedTable.prototype._fixTable = function(_){
if(_.obj.rows.length <= 0){
return false;
}
var hasLeft = _.buildLeft();
var hasHead = _.buildHead();
_.box.onscroll = function(){
if(_.divHead != null){
_.divHead.scrollLeft = this.scrollLeft;
}
if(_.divLeft != null){
_.divLeft.scrollTop = this.scrollTop;
}
};
if(hasHead && hasLeft){
_.buildTopLeft();
}
};
oFixedTable.prototype.buildHead = function(){
var _ = this;
var strDivId = _.id + '_div_head';
var strTbId = _.id + '_tb_header';
var div = document.createElement('div');
div.id = strDivId;
div.style.cssText = 'position:absolute;overflow:hidden;z-index:' + (_.config.zindex + 1) + ';';
div.innerHTML = '<table id="' + strTbId + '" cellpadding="0" cellspacing="0" style="background:' + _.config.background + ';"></table>';
_.box.insertBefore(div, _.obj);
_.divHead = div;
_.tbHead = document.getElementById(strTbId);
//判断是否出现纵向滚动条,若出现,高度减去滚动条宽度 16px
var sw = _.obj.offsetHeight > _.box.offsetHeight ? 16 : 0;
_.divHead.style.width = (_.box.offsetWidth - sw) + 'px';
_.tbHead.style.textAlign = _.obj.style.textAlign;
_.tbHead.style.width = _.obj.offsetWidth + 'px';
var hasHead = false;
if(_.config.fixHead && _.obj.tHead != null){
var tHead = _.obj.tHead;
_.tbHead.appendChild(tHead.cloneNode(true));
hasHead = true;
} else {
for(var i=0; i<_.config.rows; i++){
var row = _.obj.rows[i];
if(row != null){
_.tbHead.appendChild(row.cloneNode(true));
hasHead = true;
}
}
}
return hasHead;
};
oFixedTable.prototype.buildLeft = function(){
var _ = this;
if(_.config.cols <= 0){
return false;
}
var strDivId = _.id + '_div_left';
var strTbId = _.id + '_tb_left';
var div = document.createElement('div');
div.id = strDivId;
div.style.cssText = 'position:absolute;overflow:hidden;z-index:' + _.config.zindex + ';';
div.innerHTML = '<table id="' + strTbId + '" cellpadding="0" cellspacing="0" style="background:' + _.config.background + ';"></table>';
_.box.insertBefore(div, _.obj);
_.divLeft = div;
_.tbLeft = document.getElementById(strTbId);
_.tbLeft.style.textAlign = _.obj.style.textAlign;
//判断是否出现横向滚动条,若出现,高度减去滚动条高度 16px
var sw = _.obj.offsetWidth > _.box.offsetWidth ? 16 : 0;
_.divLeft.style.height = (_.box.offsetHeight - sw) + 'px';
var hasLeft = false;
for(var i=0,rows=_.obj.rows.length; i<rows; i++){
var row = _.tbLeft.insertRow(_.tbLeft.rows.length);
row.style.cssText = _.obj.rows[i].style.cssText;
for(j=0; j<_.config.cols; j++){
var cell = _.obj.rows[i].cells[j];
if(cell != null){
row.appendChild(cell.cloneNode(true));
cell.style.cssText = _.obj.rows[i].cells[j].style.cssText;
hasLeft = true;
}
}
}
return hasLeft;
};
oFixedTable.prototype.buildTopLeft = function(){
var _ = this;
var strDivId = _.id + '_div_top_left';
var strTbId = _.id + '_tb_top_left';
var div = document.createElement('div');
div.id = strDivId;
div.style.cssText = 'position:absolute;overflow:hidden;z-index:' + (_.config.zindex + 2) + ';';
div.innerHTML = '<table id="' + strTbId + '" cellpadding="0" cellspacing="0" style="background:' + _.config.background + ';"></table>';
_.box.insertBefore(div, _.obj);
var tbTopLeft = document.getElementById(strTbId);
tbTopLeft.style.textAlign = _.obj.style.textAlign;
for(var i=0; i<_.config.rows; i++){
var row = tbTopLeft.insertRow(tbTopLeft.rows.length);
row.style.cssText = _.obj.rows[i].style.cssText;
for(j=0; j<_.config.cols; j++){
var cell = _.obj.rows[i].cells[j];
if(cell != null){
row.appendChild(cell.cloneNode(true));
cell.style.cssText = _.obj.rows[i].cells[j].style.cssText;
hasLeft = true;
}
}
}
};
</script>
</head>
<body>
固定表头、固定左边单元格 示例:
<br />
<div id="divBox1" style="margin:20px 50px;overflow:auto;height:520px;width:450px;float:left;"></div>
</body>
<div id="divBox2" style="margin:20px 50px;overflow:auto;height:520px;width:450px;float:left;"></div>
</body>
</html>
<script type="text/javascript">
function test(num){
var box = document.getElementById('divBox' + num);
var strHtml = '<table id="tbTest' + num + '" cellpadding="0" cellspacing="0" style="text-align:center;width:680px;">';
for(var i=0; i<1; i++){
strHtml += '<tr style="background:url(title_bg.png) repeat-x;">';
for(var j=0; j<8; j++){
if(j < 8-1){
strHtml += '<th style="width:80px;">';
} else {
strHtml += '<th>';
}
strHtml += '第' + (i+1) + '行第' + (j+1) + '列';
strHtml += '</th>';
}
strHtml += '</tr>';
}
for(var i=1; i<30; i++){
strHtml += '<tr>';
for(var j=0; j<8; j++){
if(j < 8-1){
strHtml += '<td style="width:80px;">';
} else {
strHtml += '<td>';
}
strHtml += '第' + (i+1) + '行第' + (j+1) + '列';
strHtml += '</td>';
}
strHtml += '</tr>';
}
strHtml += '</table>';
box.innerHTML = strHtml;
}
test(1);
test(2);
//调用固定表头类
var ofix1 = new oFixedTable('ofix1', document.getElementById('tbTest1'), {rows:1, cols:1});
var ofix2 = new oFixedTable('ofix2', document.getElementById('tbTest2'), {rows:2, cols:1});
</script>
http://www.miniui.com/demo/index.html#src=datagrid/fixedcolumns.html
注:以上内容均来自网络。用过之后没记录原地址,见谅。