2018年01月05日 16:55:07 小貓兒 閱讀數:4419css
方法1:js實現
<!DOCTYPE HTML>
html
<html>
測試
<head>
ui
<meta charset="utf-8"/>
spa
<title>table表頭固定,不隨豎向滾動條滾動,但隨橫向滾動條滾動</title>
.net
<style type="text/css">
code
*{
htm
margin:0;
blog
padding:0;
事件
}
td, th{
width:200px;
line-height: 30px;
text-align:center;
}
th {
background-color: green;
}
td:first-child{
background-color: yellow;
}
table{
width:2000px;
}
#wrap{
position:relative;
}
#topic{
position:absolute;
top:0;
left:0;
width:2000px;
}
</style>
<script>
var pre_scrollTop = 0; // 滾動條事件以前文檔滾動高度
var obj_topic ;
window.onload = function(){
pre_scrollTop = (document.documentElement.scrollTop || document.body.scrollTop);
obj_topic = document.getElementById("topic");
}
window.onscroll = function(){
if(pre_scrollTop != (document.documentElement.scrollTop || document.body.scrollTop)){
pre_scrollTop = (document.documentElement.scrollTop || document.body.scrollTop);
obj_topic.style.top = pre_scrollTop+"px";
}
}
</script>
</head>
<body>
<div id="wrap">
<table cellpadding="0" cellspacing="0">
<tr id="topic">
<th>000</th>
<th>001</th>
<th>002</th>
<th>003</th>
<th>004</th>
<th>005</th>
<th>006</th>
<th>007</th>
<th>008</th>
<th>009</th>
</tr>
<tr>
<td>100</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>101</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>102</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>103</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>104</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>105</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>106</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>107</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>108</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>109</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>110</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>111</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>112</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>113</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>114</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>115</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>116</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>117</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>118</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>119</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>120</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>121</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>122</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>123</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>124</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>125</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>126</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>127</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>128</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>129</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>130</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>131</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>132</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>133</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>134</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>135</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>136</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>137</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>138</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>139</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
<tr>
<td>140</td><td>content1</td><td>content2</td><td>content3</td><td>content4</td><td>content5</td><td>content6</td><td>content7</td><td>content8</td><td>content9</td>
</tr>
</table>
</div>
</body>
</html>
效果:
方法二:css樣式實現(推薦,較簡單!!!!!!!!!!!!!!!!!!!)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
table tbody {
display: block;
height: 200px;
overflow-y: scroll;
}
table thead,
table tbody tr {
display: table;
width: 100%;
table-layout: fixed;
text-align: center;
}
thead th,
tbody td {
width: 50px;
}
table thead {
width: calc( 100% - 1em);
}
</style>
</head>
<body>
<div style="width: 800px;">
<div class="table-head">
<table align="center">
<thead>
<tr>
<th>序號</th>
<th>內容</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>2</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>3</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>4</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>5</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>6</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>7</td>
<td>我只是用來測試的</td>
</tr>
<tr>
<td>8</td>
<td>我只是用來測試的</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
效果圖: