table--头部固定--数列滚动

博客提及探究效果如何,但未给出更多关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .table-responsive {
      width: 800px !important;
      overflow-y: auto !important;
      overflow-x: hidden;
    }

    .table-th-css {

      position: relative !important;
      text-align: center;
      top: 0;
    }

    .section-scroll {

      height: calc(100vh - 100px);
    }
  </style>
  <link rel="stylesheet" href="css/swiper.css">
  <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.bootcss.com/animate.css/3.7.2/animate.min.css" rel="stylesheet">
  <script src="https://cdn.bootcss.com/wow/1.1.2/wow.min.js"></script>
</head>

<body>
  <div class="table-responsive section-scroll">
    <table class="table">
      <thead class="thead-dark">
        <tr>
          <th scope="col" class="table-th-css">#</th>
          <th scope="col" class="table-th-css">First</th>
          <th scope="col" class="table-th-css">Last</th>
          <th scope="col" class="table-th-css">Handle</th>
        </tr>
      </thead>
      <tbody>


      </tbody>
    </table>
  </div>
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
<script>
  var str = ""
  for (let i = 0; i < 20; i++) {
    str += `   <tr class="text-center" >
        <th scope="row">1</th>
        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>
      </tr> `

  }
  $('tbody').append(str)

  var tableCont = $('.section-scroll tr th'); //获取th
  var tableScroll = $('.section-scroll'); //获取滚动条同级的class
  function scrollHandle() {
    var scrollTop = tableScroll.scrollTop();
    // 当滚动距离大于0时设置top及相应的样式
    if (scrollTop > 0) {
      tableCont.css({
        "top": scrollTop + 'px',
        "marginTop": "0px",
      });

    } else {
      // 当滚动距离小于0时设置top及相应的样式
      tableCont.css({
        "top": scrollTop + 'px',
        "marginTop": "0",
        "border": "none",
        "marginBottom": 0
      });

    }
  }
  tableScroll.on('scroll', scrollHandle);

</script>

</html>

效果如何下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值