實現表頭固定,tbody垂直滾動
準備工做:javascript
function setIframeHeight() { var screenHeight = document.documentElement.clientHeight; }
2.屏幕旋轉觸發事件css
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方內容。循環獲取tbody第一行單元格的寬度,給thead的單元格,使表頭對齊html
<!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>
思路:監聽滾動事件,動態控制表頭位置java
<!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文件並實現觸加載更多數據,合計行jquery
<!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:沒法橫向滾動;安卓:無問題ios