完成表头牢固,tbody垂直转动
准备工作:
- 猎取页面但是地区高度
function setIframeHeight() {
var screenHeight = document.documentElement.clientHeight;
}
2.屏幕扭转触发事宜
window.addEventListener("resize",()=>{
//一般方向或许屏幕扭转180°
if(window.orientation===180||window.orientation===0){
console.log('竖屏');
}
//屏幕顺时针扭转90°或许逆时针扭转90°
if (window.orientation===90||window.orientation===-90) {
console.log('横屏');
}
})
要领一:两个table
思绪:第一个table放表头,第二个table方内容。轮回猎取tbody第一行单元格的宽度,给thead的单元格,使表头对齐
<!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>两个table</title>
<link rel="stylesheet" href="css/css.css">
<script src="js/jquery.min.js"></script>
<style>
.scrollX{overflow-x: scroll}
tr th,td {
border: 1px solid #000;
padding: 8px;
white-space: nowrap;
}
.table-head {
padding-right: 17px;
background-color: #eeeeee;
color: #000;
}
.table-body {
display: block;
height: 300px;
overflow-y: scroll;
}
.table-head table,
.table-body table {
border-collapse: collapse;
width: 100%;
}
.table-body table tr:nth-child(2n+1) {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="scrollX">
<table class="table-head">
<thead>
<tr>
<th>部门</th>
<th>用户称号</th>
<th>1月</th>
<th>2月</th>
<th>3月</th>
<th>4月</th>
<th>5月</th>
<th>6月</th>
<th>7月</th>
<th>8月</th>
<th>9月</th>
<th>10月</th>
<th>11月</th>
<th>12月</th>
<th>算计</th>
</tr>
</thead>
</table>
<table class="table-body">
<tbody>
<tr>
<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>这是一个表格内容</td>
<td>这是一个表格内容</td>
<td>这是一个表格内容</td>
</tr>
<!-- 轮回屡次tr...-->
</tbody>
</table>
</div>
<script>
//内容第一行
var bodyTR = $(".table-body tr:eq(1) td");
// 一行有多少个单元格
var bodyTRLength = bodyTR.length;
//宽度
var autoW = $('.table-body tbody').width()
var headTR = $(".table-head tr th")
//猎取表头高度
var headH=$(".table-head").height();
function setIframeHeight() {
var screenHeight = document.documentElement.clientHeight-headH;
$(".table-body").height(screenHeight);
}
//屏幕发作扭转从新盘算高度
window.addEventListener("resize",()=>{
if(window.orientation===180||window.orientation===0){
setIframeHeight();
}if (window.orientation===90||window.orientation===-90) {
setIframeHeight();
}
})
$(function () {
setIframeHeight();
$(".table-head").css("width",autoW+1);
$(".table-body").css("width",autoW+18);
for (var i = 1; i < bodyTRLength; i++) {
var tdW = $(".table-body tr:eq(1) td:eq("+i+")").width()+1;
$(".table-head tr th:eq(" + (i ) + ")").css("width", tdW)
}
})
</script>
</table>
</body>
</html>
第二种要领:经由过程JS完成
思绪:监听转动事宜,动态控制表头位置
<!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>JS 完成表头tbody牢固转动</title>
<script src="js/jquery.min.js"></script>
<link rel="stylesheet" href="css/css.css" />
<style>
.table tr th,td {
border: 1px solid #000;
}
.table-responsive {
overflow-y: scroll;
}
.text-center td {
white-space: nowrap;
padding: 10px;
}
.table-th-css {
background: #efeff4 !important;
position: relative !important;
text-align: center;
top: 0;
white-space: nowrap;
}
.table-th-css div {
border-top: none;
white-space: nowrap;
padding: 10px;
}
.section-scroll {
height: 417px;
}
</style>
</head>
<body>
<div class="table-responsive section-scroll">
<table class="table table-bordered">
<thead class="table-header">
<tr>
<th class="table-th-css">
<div>部门</div>
</th>
<th class="table-th-css">
<div>用户称号</div>
</th>
<th class="text-center table-th-css">
<div>1月</div>
</th>
<th class="text-center table-th-css">
<div>2月</div>
</th>
<th class="text-center table-th-css">
<div>3月</div>
</th>
<th class="text-center table-th-css">
<div>4月</div>
</th>
<th class="text-center table-th-css">
<div>5月</div>
</th>
<th class="text-center table-th-css">
<div>6月</div>
</th>
<th class="text-center table-th-css">
<div>7月</div>
</th>
<th class="text-center table-th-css">
<div>8月</div>
</th>
<th class="text-center table-th-css">
<div>9月</div>
</th>
<th class="text-center table-th-css">
<div>10月</div>
</th>
<th class="text-center table-th-css">
<div>11月</div>
</th>
<th class="text-center table-th-css">
<div>12月</div>
</th>
<th class="text-center table-th-css">
<div>算计</div>
</th>
</tr>
</thead>
<tbody>
<tr class="text-center">
<td>这是一个表格内容</td>
<td class="table-textWidth">这是一个表格内容</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>
<td>这是一个表格内容</td>
</tr>
<!-- 轮回屡次tr...-->
</tbody>
</table>
</div>
<script>
$(function() {
setIframeHeight();
});
//屏幕发作扭转从新盘算高度
window.addEventListener("resize",()=>{
if(window.orientation===180||window.orientation===0){
setIframeHeight();
}if (window.orientation===90||window.orientation===-90) {
setIframeHeight();
}
})
var tableCont = $(".section-scroll tr th");
//猎取th
var tableCont_child = $(".section-scroll tr th div");
//猎取th下边的div
var tableScroll = $(".section-scroll");
//猎取转动条同级的class
function scrollHandle() {
console.log(1)
var scrollTop = tableScroll.scrollTop();
// 当转动间隔大于0时设置top及响应的款式
if (scrollTop > 0) {
tableCont.css({"top": scrollTop + "px", "padding":"0"
});
tableCont_child.css({"borderTop": "1px solid #000","borderBottom": "1px solid #000","marginTop": "-1px","padding": "8px"
});
} else {
// 当转动间隔小于0时设置top及响应的款式
tableCont.css({ "top": scrollTop + "px", "marginTop": "0" });
tableCont_child.css({ "border": "none", "marginTop": 0, "marginBottom": 0, })
}
}
tableScroll.on("scroll", scrollHandle);
//猎取但是地区高度
function setIframeHeight() {
var screenHeight = document.documentElement.clientHeight;
$(".section-scroll").height(screenHeight);
}
</script>
</body>
</html>
第三种要领 经由过程easyui完成
思绪,援用easyui文件并完成触加载更多数据,算计行
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Basic DataGrid - jQuery EasyUI Mobile Demo</title>
<link rel="stylesheet" type="text/css" href="easyui/themes/metro/easyui.css">
<link rel="stylesheet" type="text/css" href="easyui/themes/mobile.css">
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.mobile.js"></script>
<!-- <script type="text/javascript" src="https://www.jeasyui.com/easyui/datagrid-bufferview.js"></script>
</head> -->
<style>
body{
margin: 0;
padding: 0;
}
.datagrid-footer td{
color: red
}
</style>
<body>
<div id="hh">
<div class="m-toolbar">
<div class="m-title">Basic DataGrid</div>
</div>
</div>
<table id="dg">
<thead>
<tr>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<!-- <th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th> -->
</tr>
</thead>
</table>
<script>
var data ={
"total":5,
"rows":[
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"AV-CB-01","productname":"Amazon Parrot","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
],
"footer":[{"productid":"算计","unitcost":"1203.00","listprice":"5895.00"}]
};
var pushdata =[
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"AV-CB-01","productname":"Amazon Parrot","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
];
$(function(){
var screenHeight = document.documentElement.clientHeight;
var pageNum=1
$("#dg").datagrid({
header:'#hh',
height:screenHeight,
data:data,
singleSelect:true,
showFooter : true,
//view:bufferview,
rownumbers:true,
pageSize:30
})
var scrollDiv = $(".datagrid-body").get(1);
console.log(pushdata)
$(scrollDiv).scroll(function(){
console.log("gundong")
var that=this;
var cango=true;
var scrollH=scrollDiv.scrollTop+scrollDiv.clientHeight
if(scrollH==scrollDiv.scrollHeight&&cango==true){
for(var i=0;i<pushdata.length;i++) {
var row_data = {
itemid : pushdata[i].itemid,
productid :pushdata[i].productid,
listprice: pushdata[i].listprice,
unitcost : pushdata[i].unitcost,
};
$('#dg').datagrid('appendRow', row_data);
}
}
})
});
//盘算表格高度
function setIframeHeight() {
var screenHeight = document.documentElement.clientHeight;
$(".table-body").height(screenHeight);
$('#dg').datagrid("resize",{
height: screenHeight
}
);
}
//屏幕发作扭转从新盘算高度
window.addEventListener("resize",()=>{
if(window.orientation===180||window.orientation===0){
setIframeHeight();
}if (window.orientation===90||window.orientation===-90) {
setIframeHeight();
}
})
</script>
</body>
</html>
优缺点剖析
EasyUI => ios:无问题;安卓:横向转动表头发抖
JS => ios:无问题;安卓:垂直转动表头发抖
两个table=> ios:没法横向转动;安卓:无问题