欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

表格的頭部固定效果通過css及jquery分別實現(xiàn)

  發(fā)布時間:2013-08-18 11:52:12   作者:佚名   我要評論
表格的頭部固定可以利用css的樣式來實現(xiàn),也可以利用jquery把原來table的頭隱藏,具體實現(xiàn)如下,感興趣的朋友可以參考下,希望對大家有所幫助
1.第一種方式利用css的樣式來實現(xiàn)表格的頭部固定

復制代碼
代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>無標題文檔</title>
<style type="text/css">
.a{
position:relative;
top:expression(this.offsetParent.scrollTop); //這里的offsetParent是最近的有固定樣式的父級元素,原理就是不斷的刷新相對于該父級元素的scrollTop來實現(xiàn)頭部固定
background:blue;
text-align:center;
z-index:10;
}
.mainDIV{
overflow:scroll;
height:100px;
}
</style>
</head>
<body>
<div class="mainDIV">
<table cellpadding="0" id="tab1" cellspacing="0" border="1" class="itemList">
<thead>
<tr style="background-color: #eeeeee; margin: 0px; line-height: 20px; font-weight: bold;
padding: 0px 0px 0px 0px;" class="a">
<td> 列1</td>
<td> 列2</td>
<td> 列3 </td>
<td> 列4</td>
</tr>
</thead>
<tbody>
...

2.第二種方法是用jquery把原來table的頭隱藏,然后復制出一個一模一樣的然后insertBefore到table前面
indes.html測試頁面

復制代碼
代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>qubernet@163.com_jQuery實現(xiàn)表頭固定效果(挺不錯的?。。。?lt;/title>
<script src="jquery-1.4.js" type="text/javascript"></script>
<script src="jquery.remainHead.js" type="text/javascript"></script>
<style type="text/css">
.itemList
{
border: solid 1px #cccccc;
overflow: hidden
width: 100%;
border-collapse: collapse;
}
.itemList td
{
padding: 0px 0px 0px 0px;
color: #444444;
border: solid 1px #cccccc;
text-align: center;
line-height: 20px;
}
</style>
<script type="text/javascript">
$(function() {
$.remainHead("tab1","div1");
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style=" height: 250px; overflow:scroll;" id="div1">
<table cellpadding="0" id="tab1" cellspacing="0" border="1" class="itemList">
<thead>
<tr style="background-color: #eeeeee; margin: 0px; line-height: 20px; font-weight: bold;
padding: 0px 0px 0px 0px;">
<td> 列1</td>
<td>列2 </td>
<td> 列3</td>
<td> 列4</td>
</tr>
</thead>
<tbody>
<tr><td>我是測試的數(shù)據行…………</td><td>我是測試的數(shù)據行…………</td><td>我是測試的數(shù)據行…………</td><td>我是測試的數(shù)據行…………</td></tr>
....

jquery.remainHead.js 頁面

復制代碼
代碼如下:

;(function($){
$.extend({
//原理就是把原來的頭給隱藏掉了,然后復制一個頭出來插入到table的上面來實現(xiàn)頭部固定
"remainHead":function(tableId,tableParentDivId){
var obj = document.getElementById("tableHeaderDiv" + tableId);
if (obj) {
jQuery(obj).remove();
}
var browserName = navigator.appName;
var ver = navigator.appVersion;
var browserVersion = parseFloat(ver.substring(ver.indexOf("MSIE") + 5, ver.lastIndexOf("Windows")));
var content = document.getElementById(tableParentDivId);
var scrollWidth = content.offsetWidth - content.clientWidth;
var tableOrg = jQuery("#" + tableId)
var table = tableOrg.clone();
table.attr("id", "cloneTable");
var tableClone = jQuery(tableOrg).find("tr").each(function() {
});
var tableHeader = jQuery(tableOrg).find("thead");
var tableHeaderHeight = tableHeader.height();
tableHeader.hide();
var colsWidths = jQuery(tableOrg).find("tbody tr:first td").map(function() {
return jQuery(this).width();
});
//alert(colsWidths.get().join(","));用get()可以獲取數(shù)組的dom元素
var tableCloneCols = jQuery(table).find("thead tr:first td")
if (colsWidths.size() > 0) {
for (i = 0; i < tableCloneCols.size(); i++) {
if (i == tableCloneCols.size() - 1) {
if (browserVersion == 8.0)
tableCloneCols.eq(i).width(colsWidths[i] + scrollWidth);
else
tableCloneCols.eq(i).width(colsWidths[i]);
} else {
tableCloneCols.eq(i).width(colsWidths[i]);
}
}
}
var headerDiv = document.createElement("div");
headerDiv.appendChild(table[0]);
jQuery(headerDiv).css("height", tableHeaderHeight);
jQuery(headerDiv).css("overflow", "hidden");
jQuery(headerDiv).css("z-index", "20");
jQuery(headerDiv).css("width", "100%");
jQuery(headerDiv).attr("id", "tableHeaderDiv" + tableId);
jQuery(headerDiv).insertBefore(tableOrg.parent());
}
});
})(jQuery);

相關文章

最新評論