Vue多種方法實(shí)現(xiàn)表頭和首列固定的示例代碼
有時(shí)表格太大,滾動(dòng)時(shí)信息查看不方便,需要對(duì)表格進(jìn)行全局表頭、首列固定,
上效果:

一、創(chuàng)建多個(gè)表格進(jìn)行覆蓋
思路:當(dāng)頁(yè)面滾動(dòng)到臨界值時(shí),出現(xiàn)固定表頭、首列
先創(chuàng)建一個(gè)活動(dòng)表格
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
th,
td {
min-width: 200px;
height: 50px;
}
#sTable {
margin-top: 300px
}
[v-cloak]{
display: none;
}
</style>
</head>
<body v-cloak>
<!--活動(dòng)的表格-->
<table id="sTable" border="1" cellspacing="0">
<thead>
<tr>
<th v-for="item in th">{{item.key}}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tl">
<td v-for="list in item">{{list.key}}</td>
</tr>
</tbody>
</table>
<script src="vue.js"></script>
<script src="jquery.js"></script>
<script>
var vm = new Vue({
el: "body",
data: function() {
return {
th: [],
tl: [],
temp: [],
}
},
methods: {
//生成表格
CTable: function() {
for(var i = 0; i < 10; i++) {
this.th.push({
key: "head" + i
})
}
for(var i = 0; i < 100; i++) {
for(var j = 0; j < 10; j++) {
this.temp.push({
key: j + "body" + i
})
}
this.tl.push(this.temp)
this.temp = []
}
},
},
ready: function() {
this.CTable();
},
})
</script>
</body>
</html>
再添加固定表頭:
#fHeader {
background: lightblue;
position: fixed;
top: 0;
}
<!--固定表頭-->
<table border="1" id="fHeader" cellspacing="0" v-show="fixedHeader">
<thead>
<tr >
<th v-for="item in th">{{item.key}}</th>
</tr>
</thead>
</table>
監(jiān)控表格位置到達(dá)窗口頂部時(shí)出現(xiàn)固定表頭
//監(jiān)控表頭位置
headerMonitor:function(){
var self=this
var hHeight=$("#sTable").offset().top;
$(document).scroll(function(){
//當(dāng)滾動(dòng)條達(dá)到偏移值的時(shí)候,出現(xiàn)固定表頭
if($(this).scrollTop()>hHeight){
self.fixedHeader=true
}else{
self.fixedHeader=false
}
})
}
當(dāng)然需要調(diào)用該方法
ready: function() {
this.CTable();
this.headerMonitor()
},
然后添加固定首列以及固定的A1單元格
#fHeader {
background: lightblue;
position: fixed;
top: 0;
z-index: 1;
}
.fixedA1{
background: lightblue;
position: fixed;
top: 0;
left: 0;
z-index:2;
}
<!--固定A1-->
<table border="1" cellspacing="0" class="fixedA1" v-show="fixedA1">
<thead>
<tr>
<th v-for="item in th" v-if="$index==0">{{item.key}}</th>
</tr>
</thead>
</table>
<!--固定首列-->
<table border="1" cellspacing="0" class="fixedCol" v-show="fixedCol">
<thead>
<tr>
<th v-for="item in th" v-if="$index==0">{{item.key}}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tl">
<td v-for="list in item" v-if="$index==0">{{list.key}}</td>
</tr>
</tbody>
</table >
同理監(jiān)控表格的位置
//監(jiān)控表頭、首列位置
monitor:function(){
var self=this
$(document).scroll(function(){
self.setPosition()
//當(dāng)滾動(dòng)條達(dá)到左偏移值的時(shí)候,出現(xiàn)固定列頭
if($(this).scrollLeft()>self.hLeft){
self.fixedCol=true
}else{
self.fixedCol=false
}
//當(dāng)滾動(dòng)條達(dá)到上偏移值的時(shí)候,出現(xiàn)固定表頭
if($(this).scrollTop()>self.hHeight){
self.fixedHeader=true
}else{
self.fixedHeader=false
}
//當(dāng)表格移到左上角時(shí),出現(xiàn)固定的A1表格
if($(this).scrollLeft()>self.hLeft&&$(this).scrollTop()>self.hHeight){
self.fixedA1=true
}else{
self.fixedA1=false
}
})
},
因?yàn)楸砀竦囊苿?dòng)會(huì)影響表頭的位置的定位位置,因此需要將當(dāng)前表格的偏移值賦給固定表頭。首列
setPosition:function(){
$("#fHeader").css("left",this.hLeft-$(document).scrollLeft())
$(".fixedCol").css("top",this.hHeight-$(document).scrollTop())
}
Jq監(jiān)控滾動(dòng)新建多個(gè)表格實(shí)現(xiàn)表頭首列固定.html
二、控制樣式實(shí)現(xiàn)固定表頭,首列
思路:當(dāng)表格達(dá)到臨界值時(shí),改變表頭,首列的樣式
首先實(shí)現(xiàn)表頭固定
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
th,
td {
min-width: 200px;
height: 50px;
}
table {
margin: 300px
}
.fHeader {
background: lightblue;
position: fixed;
top: 0;
}
[v-cloak]{
display: none;
}
</style>
</head>
<body v-cloak>
<table border="1" cellspacing="0">
<thead>
<tr :class="{fHeader:fixedHeader}">
<th v-for="item in th">{{item.key}}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tl">
<td v-for="list in item">{{list.key}}</td>
</tr>
</tbody>
</table>
<script src="vue.js"></script>
<script src="jquery.js"></script>
<script>
var vm = new Vue({
el: "body",
data: function() {
return {
th: [],
tl: [],
temp: [],
fixedHeader: false,
}
},
methods: {
//生成表格,代碼相同,省略
CTable: function() {},
//監(jiān)控表頭位置
headerMonitor:function(){
var self=this
var hHeight=$("table").offset().top;
$(document).scroll(function(){
//當(dāng)滾動(dòng)條達(dá)到偏移值的時(shí)候,出現(xiàn)固定表頭
if($(this).scrollTop()>hHeight){
self.fixedHeader=true
}else{
self.fixedHeader=false
}
})
}
},
ready: function() {
this.CTable();
this.headerMonitor()
},
})
</script>
</body>
</html>
添加固定首列
.fixedCol>:first-child{
background: lightblue;
position: fixed;
z-index: 1;
border:1px solid grey;
left: 0;
line-height: 50px;
}
監(jiān)控表格位置
//監(jiān)控表頭,首列位置
monitor:function(){
this.setPosition()
var self=this
$(document).scroll(function(){
self.setPosition();
//當(dāng)滾動(dòng)條達(dá)到偏移值的時(shí)候,出現(xiàn)固定表頭
if($(this).scrollTop()>self.hHeight){
self.fixedHeader=true;
}else{
self.fixedHeader=false
}
//當(dāng)滾動(dòng)條達(dá)到左偏移值的時(shí)候,出現(xiàn)固定列頭
if($(this).scrollLeft()>self.hLeft){
self.fixedCol=true
}else{
self.fixedCol=false
}
//當(dāng)表格移到左上角時(shí),出現(xiàn)固定的A1表格
if($(this).scrollLeft()>self.hLeft&&$(this).scrollTop()>self.hHeight){
self.fixedA1=true
}else{
self.fixedA1=false
}
})
},
設(shè)置偏移值
//使固定表頭與列頭的偏差與當(dāng)前表格的偏移值相等
setPosition:function(){
$(".fixedHeader").css("left",this.hLeft-$(document).scrollLeft())
for(var i=0,len=this.tl.length+1;i<len;i++){
//因?yàn)樵O(shè)置了“border-collapse:collapse”,所以要加“54-1”
$(".fixedCol>:first-child").eq(i).css("top",this.hHeight-$(document).scrollTop()+53*i)
}
}
因?yàn)楫?dāng)表頭變成fixed定位時(shí)會(huì)脫離文檔流,表格的第二行會(huì)被隱藏,所以需要多第二列進(jìn)行寬高的拓展
/*因?yàn)閒ixed定位不占位,當(dāng)固定表頭出現(xiàn)時(shí),有一行會(huì)補(bǔ)到表頭位置,即有一行跳空,將tbody的第一行行高加倍*/
.fixedHeaderGap:first-child>td{
padding-top:54px;
}
/*因?yàn)閒ixed定位不占位,當(dāng)固定列頭出現(xiàn)時(shí),有一列會(huì)補(bǔ)到列頭位置,即有一列跳空,將tbody的第二列p設(shè)置padding*/
.fixedCol>:nth-child(2){
padding-left: 205px;
}
當(dāng)再次瀏覽器打開時(shí)該頁(yè)面時(shí),需要監(jiān)控表格是否還達(dá)到固定表頭的臨界條件
watch:{
//頁(yè)面初始加載時(shí),使固定表頭與列頭的偏差與當(dāng)前表格的偏移值相等
"fixedHeader":function(){
this.setPosition()
},
"fixedCol":function(){
this.setPosition()
},
},
三、Vue自定義指令實(shí)現(xiàn)滾動(dòng)監(jiān)聽
當(dāng)使用vue時(shí),就很少會(huì)用到Jq這么龐大的庫(kù),而且vue官方也不推薦操作Dom元素,因此可以自定義指令實(shí)現(xiàn)固定表頭,首列。本文用的是Vue.js v1.0.26,但V2.0的思路其實(shí)也一樣。
直接上代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
th,
td {
min-width: 200px;
height: 50px;
}
#sTable {
margin: 300px
}
.fixedCol{
position: fixed;
left: 0;
background: lightblue;
z-index: 1;
}
#fHeader {
background: lightblue;
position: fixed;
top: 0;
z-index: 1;
}
.fixedA1{
background: lightblue;
position: fixed;
top: 0;
left: 0;
z-index:2;
}
[v-cloak]{
display: none;
}
</style>
</head>
<body v-cloak>
<!--固定A1-->
<table border="1" cellspacing="0" class="fixedA1" v-show="fixedA1">
<thead>
<tr>
<th v-for="item in th" v-if="$index==0">{{item.key}}</th>
</tr>
</thead>
</table>
<!--固定列頭-->
<table border="1" cellspacing="0" class="fixedCol" v-show="fixedCol">
<thead>
<tr>
<th v-for="item in th" v-if="$index==0">{{item.key}}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tl">
<td v-for="list in item" v-if="$index==0">{{list.key}}</td>
</tr>
</tbody>
</table >
<!--固定表頭-->
<table border="1" id="fHeader" cellspacing="0" v-show="fixedHeader">
<thead>
<tr >
<th v-for="item in th">{{item.key}}</th>
</tr>
</thead>
</table>
<!--活動(dòng)的表格,綁定自定義指令-->
<table id="sTable" border="1" cellspacing="0" v-scroll>
<thead>
<tr>
<th v-for="item in th">{{item.key}}</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tl">
<td v-for="list in item">{{list.key}}</td>
</tr>
</tbody>
</table>
<script src="vue.js"></script>
<script>
var vm = new Vue({
el: "body",
data: function() {
return {
th: [],
tl: [],
temp: [],
fixedCol: false,
fixedHeader:false,
fixedA1:false,
hLeft:0,
hHeight:0,
}
},
directives:{
scroll:{
bind:function(){
//觸發(fā)滾動(dòng)監(jiān)聽事件
window.addEventListener('scroll',function(){
this.vm.monitor()
})
}
}
},
methods: {
//生成表格
CTable: function() {},
//監(jiān)控表頭、列頭位置
monitor:function(){
this.setPosition();
//當(dāng)滾動(dòng)條達(dá)到左偏移值的時(shí)候,出現(xiàn)固定列頭
if(document.body.scrollLeft>this.hLeft){
this.fixedCol=true;
}else{
this.fixedCol=false;
}
//當(dāng)滾動(dòng)條達(dá)到上偏移值的時(shí)候,出現(xiàn)固定表頭
if(document.body.scrollTop>this.hHeight){
this.fixedHeader=true;
}else{
this.fixedHeader=false;
}
//當(dāng)表格移到左上角時(shí),出現(xiàn)固定的A1表格
if(document.body.scrollLeft>this.hLeft&&document.body.scrollTop>this.hHeight){
this.fixedA1=true;
}else{
this.fixedA1=false;
}
},
//使固定表頭與列頭的偏差與當(dāng)前表格的偏移值相等
setPosition:function(){
document.getElementById("fHeader").style.left=this.hLeft-document.body.scrollLeft+"px";
document.getElementsByClassName("fixedCol")[0].style.top=this.hHeight-document.body.scrollTop+"px";
},
},
ready: function() {
this.CTable();
this.hLeft=document.getElementById("sTable").offsetLeft;
this.hHeight=document.getElementById("sTable").offsetTop
this.monitor()
},
})
</script>
</body>
</html>
若想要做成自定義回調(diào)事件,可以用eval(),
<table id="sTable" border="1" cellspacing="0" v-scroll="monitor">
directives:{
scroll:{
bind:function(){
var self=this;
//觸發(fā)滾動(dòng)監(jiān)聽事件
window.addEventListener('scroll',function(){
//觸發(fā)滾動(dòng)回調(diào)事件
eval("self.vm."+self.expression)()
})
}
}
},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+element-ui動(dòng)態(tài)生成多級(jí)表頭的方法
- vue elementUI table 自定義表頭和行合并的實(shí)例代碼
- vue element-ui table組件動(dòng)態(tài)生成表頭和數(shù)據(jù)并修改單元格格式 父子組件通信
- Vue+elementui 實(shí)現(xiàn)復(fù)雜表頭和動(dòng)態(tài)增加列的二維表格功能
- Vue+Element自定義縱向表格表頭教程
- vue element 中的table動(dòng)態(tài)渲染實(shí)現(xiàn)(動(dòng)態(tài)表頭)
- vue el-table實(shí)現(xiàn)自定義表頭
- Vue 固定頭 固定列 點(diǎn)擊表頭可排序的表格組件
- vue實(shí)現(xiàn)element表格里表頭信息提示功能(推薦)
- vue+elementui實(shí)現(xiàn)表格多級(jí)表頭效果
相關(guān)文章
解決Vue-Router升級(jí)導(dǎo)致的Uncaught (in promise)問(wèn)題
這篇文章主要介紹了解決Vue-Router升級(jí)導(dǎo)致的Uncaught (in promise)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue3組件庫(kù)Shake抖動(dòng)組件搭建過(guò)程詳解
這篇文章主要為大家介紹了vue3組件庫(kù)Shake抖動(dòng)組件搭建過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
解決vue中修改export default中腳本報(bào)一大堆錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決vue中修改export default中腳本報(bào)一大堆錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
使用Vue實(shí)現(xiàn)Markdown文檔的展示和解析
在Vue項(xiàng)目中,Markdown文檔的使用越來(lái)越普遍,因此在Vue中如何進(jìn)行Markdown文檔展示與解析也成為了一個(gè)熱門話題,本文將介紹如何使用Vue實(shí)現(xiàn)Markdown文檔的展示和解析,文中通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-01-01
Element的Message彈窗重復(fù)彈出問(wèn)題解決
本文主要介紹了Element的Message彈窗重復(fù)彈出,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue項(xiàng)目中封裝echarts的優(yōu)雅方式分享
在實(shí)際項(xiàng)目開發(fā)中,我們會(huì)經(jīng)常與圖表打交道,比如?訂單數(shù)量表、商品銷量表、會(huì)員數(shù)量表等等,它可能是以折線圖、柱狀圖、餅狀圖等等的方式來(lái)展現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中封裝echarts的優(yōu)雅方式的相關(guān)資料,需要的朋友可以參考下2022-03-03
Vue3 axios配置以及cookie的使用方法實(shí)例演示
這篇文章主要介紹了Vue3 axios配置以及cookie的使用方法,需要的朋友可以參考下2023-02-02

