站长资源网络编程

Vue多种方法实现表头和首列固定的示例代码

整理:jimmy2026/7/31浏览2
简介有时表格太大,滚动时信息查看不方便,需要对表格进行全局表头、首列固定, 上效果: 一、创建多个表格进行覆盖思路:当页面滚动到临界值时,出现固定表头、首列先创建一个活动表格</title</div> <div class="news_infos"><div id="MyContent"><p>有时表格太大,滚动时信息查看不方便,需要对表格进行全局表头、首列固定, <br> </p> <p>上效果: <br> </p> <p><a href="javascript:;" onclick="showimg('/UploadFiles/2021-04-02/20182291956123.gif');"><img src="/UploadFiles/2021-04-02/20182291956123.gif" alt="Vue多种方法实现表头和首列固定的示例代码" onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" border="0"/></a></p> <p><strong>一、创建多个表格进行覆盖</strong></p> <p>思路:当页面滚动到临界值时,出现固定表头、首列<br> </p> <p>先创建一个活动表格<br> </p> <div class="htmlcode"> <pre class="brush:xhtml;"> <!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> <!--活动的表格--> <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="/UploadFiles/2021-04-02/vue.js"> <p>再添加固定表头:</p> <div class="htmlcode"> <pre class="brush:css;"> #fHeader { background: lightblue; position: fixed; top: 0; }</pre> </div> <div class="htmlcode"> <pre class="brush:xhtml;"> <!--固定表头--> <table border="1" id="fHeader" cellspacing="0" v-show="fixedHeader"> <thead> <tr > <th v-for="item in th">{{item.key}}</th> </tr> </thead> </table></pre> </div> <p>监控表格位置到达窗口顶部时出现固定表头</p> <div class="htmlcode"> <pre class="brush:js;"> //监控表头位置 headerMonitor:function(){ var self=this var hHeight=$("#sTable").offset().top; $(document).scroll(function(){ //当滚动条达到偏移值的时候,出现固定表头 if($(this).scrollTop()>hHeight){ self.fixedHeader=true }else{ self.fixedHeader=false } }) } </pre> </div> <p>当然需要调用该方法</p> <div class="htmlcode"> <pre class="brush:js;"> ready: function() { this.CTable(); this.headerMonitor() },</pre> </div> <p>然后添加固定首列以及固定的A1单元格</p> <div class="htmlcode"> <pre class="brush:css;"> #fHeader { background: lightblue; position: fixed; top: 0; z-index: 1; } .fixedA1{ background: lightblue; position: fixed; top: 0; left: 0; z-index:2; } </pre> </div> <div class="htmlcode"> <pre class="brush:xhtml;"> <!--固定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 > </pre> </div> <p>同理监控表格的位置</p> <div class="htmlcode"> <pre class="brush:js;"> //监控表头、首列位置 monitor:function(){ var self=this $(document).scroll(function(){ self.setPosition() //当滚动条达到左偏移值的时候,出现固定列头 if($(this).scrollLeft()>self.hLeft){ self.fixedCol=true }else{ self.fixedCol=false } //当滚动条达到上偏移值的时候,出现固定表头 if($(this).scrollTop()>self.hHeight){ self.fixedHeader=true }else{ self.fixedHeader=false } //当表格移到左上角时,出现固定的A1表格 if($(this).scrollLeft()>self.hLeft&&$(this).scrollTop()>self.hHeight){ self.fixedA1=true }else{ self.fixedA1=false } }) },</pre> </div> <p>因为表格的移动会影响表头的位置的定位位置,因此需要将当前表格的偏移值赋给固定表头。首列</p> <div class="htmlcode"> <pre class="brush:js;"> setPosition:function(){ $("#fHeader").css("left",this.hLeft-$(document).scrollLeft()) $(".fixedCol").css("top",this.hHeight-$(document).scrollTop()) }</pre> </div> <p>Jq监控滚动新建多个表格实现表头首列固定.html</p> <p><strong>二、控制样式实现固定表头,首列</strong></p> <p>思路:当表格达到临界值时,改变表头,首列的样式<br> </p> <p>首先实现表头固定</p> <div class="htmlcode"> <pre class="brush:xhtml;"> <!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="/UploadFiles/2021-04-02/vue.js"> <p>添加固定首列</p> <div class="htmlcode"> <pre class="brush:css;"> .fixedCol>:first-child{ background: lightblue; position: fixed; z-index: 1; border:1px solid grey; left: 0; line-height: 50px; }</pre> </div> <p>监控表格位置</p> <div class="htmlcode"> <pre class="brush:js;"> //监控表头,首列位置 monitor:function(){ this.setPosition() var self=this $(document).scroll(function(){ self.setPosition(); //当滚动条达到偏移值的时候,出现固定表头 if($(this).scrollTop()>self.hHeight){ self.fixedHeader=true; }else{ self.fixedHeader=false } //当滚动条达到左偏移值的时候,出现固定列头 if($(this).scrollLeft()>self.hLeft){ self.fixedCol=true }else{ self.fixedCol=false } //当表格移到左上角时,出现固定的A1表格 if($(this).scrollLeft()>self.hLeft&&$(this).scrollTop()>self.hHeight){ self.fixedA1=true }else{ self.fixedA1=false } }) }, </pre> </div> <p>设置偏移值</p> <div class="htmlcode"> <pre class="brush:js;"> //使固定表头与列头的偏差与当前表格的偏移值相等 setPosition:function(){ $(".fixedHeader").css("left",this.hLeft-$(document).scrollLeft()) for(var i=0,len=this.tl.length+1;i<len;i++){ //因为设置了“border-collapse:collapse”,所以要加“54-1” $(".fixedCol>:first-child").eq(i).css("top",this.hHeight-$(document).scrollTop()+53*i) } } </pre> </div> <p>因为当表头变成fixed定位时会脱离文档流,表格的第二行会被隐藏,所以需要多第二列进行宽高的拓展</p> <div class="htmlcode"> <pre class="brush:css;"> /*因为fixed定位不占位,当固定表头出现时,有一行会补到表头位置,即有一行跳空,将tbody的第一行行高加倍*/ .fixedHeaderGap:first-child>td{ padding-top:54px; } /*因为fixed定位不占位,当固定列头出现时,有一列会补到列头位置,即有一列跳空,将tbody的第二列p设置padding*/ .fixedCol>:nth-child(2){ padding-left: 205px; }</pre> </div> <p>当再次浏览器打开时该页面时,需要监控表格是否还达到固定表头的临界条件</p> <div class="htmlcode"> <pre class="brush:js;"> watch:{ //页面初始加载时,使固定表头与列头的偏差与当前表格的偏移值相等 "fixedHeader":function(){ this.setPosition() }, "fixedCol":function(){ this.setPosition() }, }, </pre> </div> <p>改样式实现固定表头首列.html</p> <p><strong>三、Vue自定义指令实现滚动监听</strong></p> <p>当使用vue时,就很少会用到Jq这么庞大的库,而且vue官方也不推荐操作Dom元素,因此可以自定义指令实现固定表头,首列。本文用的是Vue.js v1.0.26,但V2.0的思路其实也一样。<br> </p> <p>直接上代码<br> </p> <div class="htmlcode"> <pre class="brush:xhtml;"> <!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> <!--活动的表格,绑定自定义指令--> <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="/UploadFiles/2021-04-02/vue.js"> <p>若想要做成自定义回调事件,可以用eval(),</p> <div class="htmlcode"> <pre class="brush:xhtml;"> <table id="sTable" border="1" cellspacing="0" v-scroll="monitor"></pre> </div> <div class="htmlcode"> <pre class="brush:js;"> directives:{ scroll:{ bind:function(){ var self=this; //触发滚动监听事件 window.addEventListener('scroll',function(){ //触发滚动回调事件 eval("self.vm."+self.expression)() }) } } }, </pre> </div> <p>自定义回调指令固定表列头.html  <br> </p> <p>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。</p></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="/3g/1/93492.html" title="使用express+multer实现node中的图片上传功能">使用express+multer实现node中的图片上传功能</a></p> <p>下一篇:<a href="/3g/1/93494.html" title="jquery.picsign图片标注组件实例详解">jquery.picsign图片标注组件实例详解</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li><a href="/3g/1/623703.html" title="群星《奔赴!万人现场 第2期》[FLAC/分轨]">群星《奔赴!万人现场 第2期》[FLAC/分轨]</a></li> <li><a href="/3g/1/623702.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623701.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623700.html" title="【古典音乐】詹姆斯·高威《季节》1993[WA">【古典音乐】詹姆斯·高威《季节》1993[WA</a></li> <li><a href="/3g/1/623699.html" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]">贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</a></li> <li><a href="/3g/1/623698.html" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE">小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE</a></li> <li><a href="/3g/1/623697.html" title="群星《欢迎来到我身边 电影原声专辑》[32">群星《欢迎来到我身边 电影原声专辑》[32</a></li> <li><a href="/3g/1/623696.html" title="群星《欢迎来到我身边 电影原声专辑》[FL">群星《欢迎来到我身边 电影原声专辑》[FL</a></li> <li><a href="/3g/1/623695.html" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低">雷婷《梦里蓝天HQⅡ》 2023头版限量编号低</a></li> <li><a href="/3g/1/623694.html" title="群星《2024好听新歌42》AI调整音效【WAV分">群星《2024好听新歌42》AI调整音效【WAV分</a></li> </ul> </div> </div> <div class="sidebar"> </div> <div class="sidebar"> 友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="http://www.paidiu.com/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap1.xml">站点地图</a> <a href="/sitemap.xml">SiteMap</a> </div> </article> <footer> <p>Design by <a href="http://m.ddrfans.com">DDR爱好者之家</a> <a href="http://m.ddrfans.com">http://m.ddrfans.com</a></p> </footer> <script src="/images3g/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>