效果圖:第一行和第一列固定
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>CSS實現表頭與列固定</title> <!--引用vue.js --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <style> .main{ width:1000px; overflow:auto; height:625px; } td,th{ border:1px solid gray; width:100px; height:30px; } th{ background-color:lightblue; } table{ table-layout:fixed; width:2000px; } td:first-child,th:first-child{ position:sticky; left:0; z-index:1; background-color:lightpink; } thead tr th{ position:sticky; top:0; } th:first-child{ z-index:2; background-color:lightblue; } </style> </head> <body> <!--新建Vue實例--> <div id="app"> <div class="main"> <table cellspacing="0"> <thead> <tr> <th>{{message}}</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>標題13</th> <th>標題14</th> <th>標題15</th> </tr> </thead> <tbody> <!--vue的 v-for的遍歷--> <tr v-for="(item,index) in 50" :key="index"> <td>demo1</td> <td>demo2</td> <td>demo3</td> <td>demo4</td> <td>demo5</td> <td>demo6</td> <td>demo7</td> <td>demo8</td> <td>demo9</td> <td>demo10</td> <td>demo11</td> <td>demo12</td> <td>demo13</td> <td>demo14</td> <td>demo15</td> </tr> </tbody> </table> </div> </div> </body> <!--把<div id="app></div>和這個標簽里面包含的所有DOM都實例化成了一個JS對象, 這個對象就是app--> <script> var app=new Vue({ el:'#app', data:{ message:'hello' }, methods:{ clickButton:function(){ this.my_data = "Wow! I'm changed!" } } }) </script> </html>
CSS樣式介紹:
1、overflow 屬性規定當內容溢出元素框時發生的事情。 值 描述 visible 默認值。內容不會被修剪,會呈現在元素框之外。 hidden 內容會被修剪,並且其余內容是不可見的。 scroll 內容會被修剪,但是瀏覽器會顯示滾動條以便查看其余的內容。 auto 如果內容被修剪,則瀏覽器會顯示滾動條以便查看其余的內容。 inherit 規定應該從父元素繼承 overflow 屬性的值。 案例: .main{ width:1000px; overflow:auto; height:625px; } 2、table-layout:fixed; 值 描述 automatic 默認。列寬度由單元格內容設定。 fixed 列寬由表格寬度和列寬度設定。 inherit 規定應該從父元素繼承 table-layout 屬性的值。 案例: table{ table-layout:fixed; width:2000px; } 3、position 屬性指定了元素的定位類型 static:HTML 元素的默認值,即沒有定位,遵循正常的文檔流對象。靜態定位的元素不會受到 top, bottom, left, right影響。 fixed:元素的位置相對於瀏覽器窗口是固定位置。即使窗口是滾動的它也不會移動:Fixed定位使元素的位置與文檔流無關,因此不占據空間。 Fixed定位的元素和其他元素重疊。 relative:相對定位元素的定位是相對其正常位置。 absolute:絕對定位的元素的位置相對於最近的已定位父元素, 如果元素沒有已定位的父元素,那么它的位置相對於<html>: sticky:sticky 英文字面意思是粘,粘貼,所以可以把它稱之為粘性定位。 指定 top, right, bottom 或 left 四個閾值其中之一,才可使粘性定位生效。否則其行為與相對定位相同。 z-index屬性:指定了一個元素的堆疊順序。一個元素可以有正數或負數的堆疊順序 案例: td:first-child,th:first-child{ position:sticky; left:0; z-index:1; background-color:lightpink; }
4、語法:box-shadow: h-shadow v-shadow blur spread color inset;
值 說明
h-shadow 必需的。水平陰影的位置。允許負值
v-shadow 必需的。垂直陰影的位置。允許負值
blur 可選。模糊距離
spread 可選。陰影的大小
color 可選。陰影的顏色。在CSS顏色值尋找顏色值的完整列表
inset 可選。從外層的陰影(開始時)改變陰影內側陰影
案例:
.table-responsive .o_list_table thead tr:nth-child(1) th:nth-child(<t t-esc="offset_index+1"/>) {
box-shadow: -1px 0px 0 #ddd inset;
}