原生js写table点击表头按钮进行排序

最近在对之前的项目进行功能增加又因为是老项目所以用的是js和jquery
在这里插入图片描述
项目的需要是点击表头的按钮进行排序
按钮是在阿里矢量图中找的点击上箭头升序排列点击下箭头降序
在这里插入图片描述
下面是整理的代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>1</title>
    <link rel="stylesheet" href="./font_qoy0poj5ux/iconfont.css" />
    <script src="./jquery-1.12.4.min.js"></script>
    <script src="./font_qoy0poj5ux/iconfont.js"></script>
    <style type="text/css">
      body {
   
        font-size: 12px;
        line-height: 25px;
      }
      tr {
   
        height: 25px;
      }
      th {
   
        font-size: 14px;
        border-bottom: solid 1px #dddddd;
        background-color: #eeeeee;
        border: solid 1px;
      }
      td {
   
        font-size: 12px;
        text-align: center;
        border: solid 1px;
      }
      .name {
   
        position: relative;
      }
      .icon-shang1 {
   
        position: absolute;
        font-size: 12%;
        top: 12%;
        height: 12px;
        line-height: 12px;
      }
      .icon-xia1 {
   
        line-height: 12px;
        height: 12px;
        bottom: 4%;
        position: absolute;
        font-size: 12%;
      }
    </style>
  </head>
  <body>
    <table
      border="0"
      id="tbl"
      style="border:solid 1px #DDDDDD;width:360px;"
      cellpadding="0"
      cellspacing="0"
    >
      <thead>
        <tr>
          <th id="th0" class="name">
            name
            <span class="iconfont icon-shang1" onclick="a(this)"></span>
            <span class="iconfont icon-xia1" onclick="bottom()"></span>
          </th>
          <th id="th1">muscle</th>
          <th id="th2">smart</th>
          <th id="th3">city</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue原生table表格表头固定,可以通过CSS的position属性和JS的scroll事件来实现。 首先,在table标签外面嵌套一个div容器,设置其样式为position: relative,用于容纳表格表头。 然后,在表格表头的tr标签上添加一个ref属性,用于在JS中获取该元素。 接下来,使用JS监听div容器的scroll事件,在事件中通过ref获取到表头元素,并获取该元素的offsetTop和scrollTop属性值。 然后,判断scrollTop是否大于或等于offsetTop,如果是则添加一个css类或样式,将表头固定在顶部;如果不是,则移除该类或样式。 最后,将改变样式的操作放在一个debounce的函数中,用于优化滚动事件的性能。 具体代码如下: <template> <div class="container" ref="container" @scroll="handleScroll"> <table class="table"> <thead> <tr ref="thead"> <th>表头1</th> <th>表头2</th> <th>表头3</th> </tr> </thead> <tbody> <tr> <td>内容1</td> <td>内容2</td> <td>内容3</td> </tr> ... </tbody> </table> </div> </template> <script> export default { methods: { handleScroll() { const container = this.$refs.container; const thead = this.$refs.thead; const offsetTop = thead.offsetTop; const scrollTop = container.scrollTop; if (scrollTop >= offsetTop) { thead.classList.add("fixed"); } else { thead.classList.remove("fixed"); } }, }, }; </script> <style> .container { position: relative; max-height: 300px; overflow-y: scroll; } .fixed { position: fixed; top: 0; left: 0; width: 100%; z-index: 999; } .table { width: 100%; } /* 省略其他样式 */ </style> 以上就是利用Vue原生实现table表格表头固定的方法。通过设置CSS样式和监听scroll事件,可以在滚动时使表头保持在页面顶部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值