利用js+css+html實(shí)現(xiàn)固定table的列頭不動(dòng)
話不多說(shuō),跟這小編來(lái)一起看下吧
1、CSS
<style type="text/css"> #scroll_head { position: absolute; display: none; } </style>
2、Javascript
<script type="text/javascript"> //該函數(shù)在上面一個(gè)table數(shù)據(jù)加載完成后調(diào)用 //把表頭的寬度設(shè)置到會(huì)滾動(dòng)的頁(yè)頭去 var copyWidth = function () { var b = $('#data_tbody').prev().find('tr:last').find('th'); var c = $('#scroll_head').find('tr:last').find('th'); for (var i = 0; i < b.length; i++) { var newWith = b.eq(i).width(); if ($.browser.msie) { newWith += 1; } c.eq(i).width(newWith); } } $(function () { $(window).scroll(function () { if ($('#data_tbody').length > 0) { var thead = $('#data_tbody').prev(); var thOffset = thead.offset(); var scTop = $(window).scrollTop(); //滾動(dòng)條相對(duì)top的位置 if (scTop > thOffset.top) { //滾動(dòng)條滾到thead及以下的位置,用臨時(shí)的thead代替顯示 $('#scroll_head').css('display', 'block'); $('#scroll_head').offset({ top: scTop, left: thOffset.left }); } else { //滾動(dòng)條滾到thead上的位置,用table的原始thead顯示 $('#scroll_head').css('display', 'none'); } } }); }); </script>
3、Html內(nèi)容
<div id="data_div"> <table> @*thead內(nèi)容及樣式同scroll_head中的thead*@ @*thead使用深背景色,避免滾動(dòng)時(shí)和tbody內(nèi)容重疊顯示*@ <thead> <tr> @*一級(jí)標(biāo)題*@ <th class="tt1" colspan="2">一級(jí)1</th> <th class="tt2" colspan="5">一級(jí)2</th> <th class="tt3" colspan="6">一級(jí)3</th> </tr> <tr> @*二級(jí)標(biāo)題*@ <th style="width: 23px;">二級(jí)11</th> <th style="width: 36px;">二級(jí)12</th> <th class="tt" style="width: 40px;">二級(jí)21</th> <th class="tt" style="width: 30px;">二級(jí)22</th> <th class="tt" style="width: 30px;">二級(jí)23</th> <th class="tt" style="width: 30px;">二級(jí)23</th> <th class="tt" style="width: 30px;">二級(jí)24</th> <th class="tt" style="width: 30px;">二級(jí)25</th> <th class="tt" style="width: 30px;">二級(jí)31</th> <th class="tt" style="width: 30px;">二級(jí)32</th> <th class="tt" style="width: 30px;">二級(jí)33</th> <th class="tt" style="width: 30px;">二級(jí)33</th> <th class="tt" style="width: 30px;">二級(jí)34</th> <th class="tt" style="width: 30px;">二級(jí)35</th> <th class="tt" style="width: 30px;">二級(jí)36</th> </tr> </thead> <tbody id="data_tbody"> 數(shù)據(jù)內(nèi)容,在數(shù)據(jù)加載完成后調(diào)用copyWidth()函數(shù)解決兼容性 </tbody> </table> </div> <div id="scroll_head" style="display:block; top: 168px; left: 0px; position: relative;"> <table width="100%"> <thead> @*thead使用深背景色,避免滾動(dòng)時(shí)和tbody內(nèi)容重疊顯示*@ <tr> @*一級(jí)標(biāo)題*@ <th class="tt1" colspan="2">一級(jí)1</th> <th class="tt2" colspan="5">一級(jí)2</th> <th class="tt3" colspan="6">一級(jí)3</th> </tr> <tr> @*二級(jí)標(biāo)題*@ <th style="width: 23px;">二級(jí)11</th> <th style="width: 36px;">二級(jí)12</th> <th class="tt" style="width: 40px;">二級(jí)21</th> <th class="tt" style="width: 30px;">二級(jí)22</th> <th class="tt" style="width: 30px;">二級(jí)23</th> <th class="tt" style="width: 30px;">二級(jí)23</th> <th class="tt" style="width: 30px;">二級(jí)24</th> <th class="tt" style="width: 30px;">二級(jí)25</th> <th class="tt" style="width: 30px;">二級(jí)31</th> <th class="tt" style="width: 30px;">二級(jí)32</th> <th class="tt" style="width: 30px;">二級(jí)33</th> <th class="tt" style="width: 30px;">二級(jí)33</th> <th class="tt" style="width: 30px;">二級(jí)34</th> <th class="tt" style="width: 30px;">二級(jí)35</th> <th class="tt" style="width: 30px;">二級(jí)36</th> </tr> </thead> </table> </div>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)點(diǎn)擊改變圖片形狀(transform應(yīng)用)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)點(diǎn)擊改變圖片形狀,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Bootstrap模態(tài)框插入視頻的實(shí)現(xiàn)代碼
這篇文章主要介紹了Bootstrap模態(tài)框插入視頻的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-06-06javascript實(shí)現(xiàn)Emrips反質(zhì)數(shù)枚舉的示例代碼
下面小編就為大家分享一篇javascript實(shí)現(xiàn)Emrips反質(zhì)數(shù)枚舉的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12window.event快達(dá)到全瀏覽器支持了,以后使用就方便了
在Tangram群里討論到<a href="#" onclick="baidu.event.preventDefault(event);">的寫法時(shí),以為標(biāo)準(zhǔn)瀏覽器只能用arguments[0]來(lái)獲取到event,結(jié)果nodiseal同學(xué)說(shuō)已經(jīng)可以這么用了,于是做了以下測(cè)試2011-11-11JS前后端實(shí)現(xiàn)身份證號(hào)驗(yàn)證代碼解析
這篇文章主要介紹了JS前后端實(shí)現(xiàn)身份證號(hào)驗(yàn)證代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07關(guān)于webpack2和模塊打包的新手指南(小結(jié))
本篇文章主要介紹了關(guān)于webpack2和模塊打包的新手指南(小結(jié)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08javascript 操作符(~、&、|、^、<<、>>)使用案例
這篇文章主要介紹了javascript 操作符(~、&、|、^、<<、>>)使用案例,非常的全面,需要的朋友可以參考下2014-12-12微信小程序?qū)崿F(xiàn)點(diǎn)擊導(dǎo)航標(biāo)簽滾動(dòng)定位到對(duì)應(yīng)位置
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)點(diǎn)擊導(dǎo)航標(biāo)簽滾動(dòng)定位到對(duì)應(yīng)位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11