vue 表头固定

利用ref操作dom元素,详情请看代码:
html:

<div class="table-scroll-box" ref="tableScrollBox">
    <div class="fixed-head" ref="tableFixedHead">
          <table>
            <thead>
              <tr>
                <th>sum</th>
                <th>Price</th>
                <th>Delta</th>
                <th>Delta_Pct</th>
                <th>Gamma</th>
                <th>Gamma_Pct</th>
                <th>Theta</th>
                <th>Theta_Pct</th>
                <th>Vega</th>
                <th>Vega_Pct</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(row, rindex) in sumList" :key="rindex" >
                <td v-for="(val, vindex) in row" :key="vindex">{{val}}</td>
              </tr>
            </tbody>
          </table>
        </div>
</div>

css:

.table-scroll-mod {
    position: relative;
    height: 65px;
    overflow: hidden;
    .table-scroll-box {
      white-space: nowrap;
      overflow: scroll;
      height: 500px;
      z-index: 1001px;
      .fixed-head {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1000;
        width: 100%;
      }

js:

mounted() {
    let self = this;
    let box = self.$refs.tableScrollBox;
    box.addEventListener('scroll', () => {
      var scrollTop = box.scrollTop;
      var fixedHead = self.$refs.tableFixedHead;
      fixedHead.scrollTop = scrollTop;
    }, false)
    
}, 

如有更好的方法,欢迎大家一起讨论

评论

暂无评论