css3表头固定,并自由拖拽列宽

来源:互联网 发布:.cn域名的ns记录 查询 编辑:程序博客网 时间:2024/06/10 17:31

一、功能要求:

1、表头固定

2、自由拖拽百变列宽


二、兼容问题:

chrome、google、360、ie11、ie10都能很好的兼容

ie9和edge滚动时,thead会上下抖动,不知大家有没有更好的解决办法


三、页面结构及功能:

<!DOCTYPE html>

<html>

<head>

<style>       

.table-wrap{ height : 200px; max-height:200px; overflow-y: scroll }        

.table {width :99% }       

.table thead th { background-color:#ddd; }   

</style>

<script src="jquery-1.8.3.min.js"></script>   

<script src="colResizable-1.6.min.js"></script>

<script>       

$(function(){           

// 表格列自由拖拽           

$(".table").colResizable({               

liveDrag:true,                 

draggingClass:"dragging",                

resizeMode:'overflow'           

});                     

// 表头固定 

$('.table-wrap').on('scroll', function(){               

var scrollTop = $(this).scrollTop();               

$('.table').find('thead tr th').css({                   

'-webkit-transform' : '-webkit-translateY(' + scrollTop + 'px)' ,                   

'-mox-transform' : '-moz-translateY(' + scrollTop + 'px)' ,                   

'-ms-transform' : '-o-translateY(' + scrollTop + 'px)' ,                   

'-o-transform' : '-ms-translateY(' + scrollTop + 'px)' ,                   

transform : 'translateY(' + scrollTop + 'px)'               

});           

});                   

});   

</script>

</head>

<body>

<div class="table-wrap">

<table class="table">

<thead>               

<tr>                   

<th>#</th>                   

<th>First Name</th>                   

<th>Last Name</th>                   

<th>Username</th>               

</tr>           

</thead>

<tbody>

<tr>                     

<th scope="row">1</th>                     

<td>Mark</td>                   

 <td>Otto</td>                     

<td>@mdo</td>               

</tr>

<tr>                     

<th scope="row">2</th>                     

<td>Jacob</td>                     

<td>Thornton</td>                     

<td>@fat</td>               

</tr>               

<tr>                   

<th scope="row">3</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">4</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">5</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">6</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">7</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">8</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">9</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">10</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">11</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">12</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>               

<tr>                   

<th scope="row">13</th>                   

<td>Larry</td>                   

<td>the Bird</td>                   

<td>@twitter</td>               

</tr>

</tbody>

<table>

</div>

</body>

</html>