The code _jquery of fixed header and column headers based on jquery

Source: Internet
Author: User
Copy Code code as follows:

<script type= "Text/javascript" src= "Http://code.jquery.com/jquery-1.6.1.min.js" ></script>
<script type= "Text/javascript" >//<! [cdata[
function fixtable (TableID, fixcolumnnumber, width, height) {
<summary>
Lock Header and column
<para> sorex.cnblogs.com </para>
</summary>
<param name= "TableID" type= "String" >
ID of the table to be locked
</param>
<param name= "Fixcolumnnumber" type= "number" >
Number of columns to lock
</param>
<param name= "width" type= "number" >
Width of display
</param>
<param name= "height" type= "number" >
Height of display
</param>
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": "A", "Background-color": "S Ilver "});
$ ("#" + 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": "4 0 "," Background-color ":" Silver "});
$ ("#" + TableID + "_tabledata"). CSS ({"Overflow": "Scroll", "width": width, "height": height, "position": "Relative", "z-i Ndex ":" 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, 600, 200);
});
]]></script>
<style><!--
. style1
{
width:137px;
}
#MyTable
{
border-top:1px solid #cc0000;
border-left:1px solid #cc0000;
}
#MyTable td,th
{border-bottom:1px solid #cc0000;
border-right:1px solid #cc0000;
}



--></style>
<p><br/> </p>
<table id= "MyTable" style= "border-bottom-color:black; Border-top-color:black; width:1000px; height:200px; Color: #000000; Border-right-color:black; Font-size:medium; Border-left-color: #15b0e1; "border=" 0 "cellspacing=" 0 "cellpadding=" 0 ">
<thead>
<tr><th style= "Text-align:center; width:80px; "rowspan=" 2 "> </th><th class=" style1 "style=" text-align:center; "rowspan=" 2 "> Project </th ><th style= "Text-align:center; width:80px "rowspan=" 2 "> Public Bulletin column </th><th style=" text-align:center; "colspan=" 9 ">dddddddd</th> <th style= "Text-align:center" rowspan= "2" colspan= "2" > Total score </th></tr>
<tr id= "Game" ><th style= "Text-align:center" colspan= "2" > Chinese mathematics </th><th style= "Text-align: Center; "colspan=" 2 "> English political </th><th style=" text-align:center; "colspan=" 2 "> Historical Geography </th><th Style= "Text-align:center" colspan= "2" > Physical Chemistry </th><th style= "Text-align:center"; width:80px; " > Biology </th></tr>
</thead>
<tbody><!--data row-->
<tr>
<td> </td>
&LT;TD class= "Style1" > </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ss </td>
&LT;TD class= "Style1" > Online time </td>
<td>1 </td>
<td>29 1</td>
<td>25 2</td>
<td>146 </td>
<td>28 </td>
<td>79 </td>
<td>73 </td>
<td>47 </td>
<td>8 </td>
<td>91 33</td>
<td>526 qq</td>
<td>dd</td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Online number </td>
<td>2 </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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td>s</td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
<tr>
<td> </td>
&LT;TD class= "Style1" > Student </td>
<td> Class 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>
<td> </td>
</tr>
</tbody>
</table>
<p> </p>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.