见代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
background: #FFFFFF; }
body .table_div_wrap {
border: 1px red solid;
background: #f0f0f0;
width: 80%;
min-width: 320px;
max-width: 600px;
margin: 0 auto;
margin-top: 100px;
position: relative;
overflow: hidden; }
body .table_div_wrap .inner .table_tbody {
width: 100%;
height: 300px;
overflow-y: auto;
overflow-x: auto; }
body .table_div_wrap .inner table {
min-width: 100%; }
body .table_div_wrap .inner table td, body .table_div_wrap .inner table th {
border-top: 1px black solid;
border-right: 1px black solid;
min-width: 150px;
height: 80px; }
body .table_div_wrap .inner table th {
border: none;
border-right: 1px black solid;
height: 30px; }
body .table_div_wrap .outer {
position: absolute;
top: 0;
left: 0; }
body .table_div_wrap .outer .table_tbody {
width: 100%;
height: 300px;
overflow-y: hidden;
overflow-x: scroll; }
body .table_div_wrap .outer table {
min-width: 100%; }
body .table_div_wrap .outer table td, body .table_div_wrap .outer table th {
border-top: 1px black solid;
border-right: 1px black solid;
min-width: 150px;
height: 80px;
display: none; }
body .table_div_wrap .outer table th {
border: none;
border-right: 1px black solid;
height: 30px; }
body .table_div_wrap .outer table th:first-child {
display: table-cell;
background: #cccccc; }
body .table_div_wrap .outer table td:first-child {
display: table-cell;
background: #cccccc; }
</style>
</head>
<body>
<div class="table_div_wrap">
<div class="inner">
<div class="table_thead">
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>first</th>
<th>second</th>
<th>third</th>
<th>fourth</th>
<th>fifth</th>
</tr>
</thead>
</table>
</div>
<div class="table_tbody">
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="outer">
<div class="table_thead">
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>first</th>
<th>second</th>
<th>third</th>
<th>fourth</th>
<th>fifth</th>
</tr>
</thead>
</table>
</div>
<div class="table_tbody">
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
<script>
var table_tbody = document.getElementsByClassName("table_tbody")[0];
var table_thead = document.getElementsByClassName("table_thead")[0];
var table_tbody_outer = document.getElementsByClassName("table_tbody")[1].getElementsByTagName('table')[0];
table_tbody.onscroll = function (e) {
// console.log(this.scrollLeft,this.scrollTop)
// console.log(table_tbody_outer)
table_thead.style.marginLeft = "-"+this.scrollLeft+"px"
table_tbody_outer.style.marginTop = "-"+this.scrollTop+'px'
}
</script>