国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > JS > 正文

利用js+css+html實(shí)現(xiàn)固定table的列頭不動(dòng)

2024-05-06 16:33:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎ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í)也希望多多支持VeVb武林網(wǎng)!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 呼玛县| 辉南县| 平利县| 荆门市| 六枝特区| 潞城市| 钟祥市| 安龙县| 改则县| 九龙城区| 都兰县| 股票| 海安县| 乡宁县| 桦南县| 吉木萨尔县| 敖汉旗| 玉林市| 绩溪县| 迭部县| 上高县| 山阳县| 深水埗区| 浑源县| 镇沅| 札达县| 广州市| 罗平县| 蓬溪县| 株洲市| 平邑县| 漳浦县| 清水河县| 永善县| 宁安市| 剑阁县| 兴业县| 会同县| 东方市| 垫江县| 荔浦县|