×

JS CSS jquery

Jquery 固定表格内滚动实例

天外来信 天外来信 发表于2012-08-08 17:00:30 浏览4356 评论0

抢沙发发表评论

有个项目需要用到很简单的表格内滚动效果。

找了几个插件,总感觉效果不满意。所以,浪费了一上午时间GOOGLE资料后,决定自己动手定制。

 

原理,利用tbhead,tbbody,tbfoot 三个层来定位。 tbody来滚动

 

 

 

HTML代码如下:

<div class="tbhead">

<table id="prolisthead">

<thead>

<tr>

<th class="tr"></th>

<th>Picture</th>

<th>Part</th>

<th>Unit</th>

<th>OE No.</th>

<th>Description</th>

<th>Use For</th>

<th>Check</th>

<th class="tl"></th>

</tr>

</thead>

</table>

</div>

<div class="tbbody">

<table id="prolist">

<thead>

<tr>

<th class="tr"></th>

<th>Picture</th>

<th>Part</th>

<th>Unit</th>

<th>OE No.</th>

<th>Description</th>

<th>Use For</th>

<th>Check</th>

<th class="tl"></th>

</tr>

</thead>

<tbody>

<tr>

<td colspan="9" align="center" ><img src="images/loading.gif"/></td>

</tr>

</tbody>

</table>

</div>

<div class="tbfoot">

<table id="prolistfoot">

<tfoot>

<tr>

<th class="br"></th><th colspan="7" id="pagenav"></th><th class="bl"></th>

</tr>

</tfoot>

</table>

</div>

 

下面的js代码放在上面的HTML后面。

<script>

function fixtable()

{

for (i = 0; i < 20; i++) 

{                        

t1=$('.tbbody thead th').eq(i).width();

//alert(t1);

if (t1!=null)

{

$('.tbhead thead th').eq(i).width(t1);

}

if (t1==null)

{

break;

}

}

}

fixtable();

$(document).ready(function(){

$('.tbbody').height($('body').height()-70-$('#import').height());

$(window).resize(function(){

 $('.tbbody').height($('body').height()-70-$('#import').height());

fixtable();

}); 

});

</script>

 

当然,还少不了CSS

主要是这两行:

.tbbody{margin-top:0px;overflow-y:auto;border:1px solid #DCE0EB}

.tbbody table{margin:-29px -1px 0 -1px;}

.tbbody thead th{}

 

 

效果看这个图片:

 

点击查看原图

 

 

评论列表

访客