vue 原生table表格 实现无限滚动效果(vueSeamlessScroll使用)

<template>
  <div class="productionCatalogueTable">
    <div class="missMaterialsTable" v-if="tableData && tableData.length > 0">
      <div class="table">
        <div class="line_th">
          <span class="th_name" style="width: 20%">册序号</span>
          <span class="th_name" style="width: 20%">航材名称</span>
          <span class="th_name" style="width: 20%">型号</span>
          <span class="th_name" style="width: 20%">装备机型</span>
          <span class="th_name" style="width: 20%">修理能力</span>
        </div>
        <div
          :style="{height: height ? height.split('rem')[0] - 3 + 'rem' : '100%'}"
          class="newStyle"
        >
          <vue-seamless-scroll
            ref="scroll"
            :data="tableData"
            :class-option="defaultOption"
            class="warp"
            :style="{
              height: height ? height.split('rem')[0] - 3 + 'rem' : '100%',
            }"
          >
            <div
              class="line_tr"
              v-for="(item, index) in tableData"
              :key="index"
            >
              <span class="th_name" style="width: 20%">
                <el-tooltip effect="dark" placement="top">
                  <div slot="content">
                    {{ item.ceSeq ? item.ceSeq : "暂无" }}
                  </div>
                  <span>{{ item.ceSeq ? item.ceSeq : "暂无" }}</span>
                </el-tooltip>
              </span>
              <span class="th_name" style="width: 20%">
                <el-tooltip effect="dark" placement="top">
                  <div slot="content">
                    {{ item.aeromatName ? item.aeromatName : "暂无" }}
                  </div>
                  <span>{{
                    item.aeromatName ? item.aeromatName : "暂无"
                  }}</span>
                </el-tooltip>
              </span>
              <span class="th_name" style="width: 20%">
                <el-tooltip effect="dark" placement="top">
                  <div slot="content">
                    {{ item.model ? item.model : "暂无" }}
                  </div>
                  <span>{{ item.model ? item.model : "暂无" }}</span>
                </el-tooltip>
              </span>
              <span class="th_name" style="width: 20%">
                <el-tooltip effect="dark" placement="top">
                  <div slot="content">
                    {{ item.aeroType ? item.aeroType : "暂无" }}
                  </div>
                  <span>{{ item.aeroType ? item.aeroType : "暂无" }}</span>
                </el-tooltip>
              </span>
              <span class="th_name" style="width: 20%">
                <el-tooltip effect="dark" placement="top">
                  <div slot="content">
                    {{ item.factoryAbility ? item.factoryAbility : "暂无" }}
                  </div>
                  <span>{{ item.factoryAbility ? item.factoryAbility : "暂无" }}</span>
                </el-tooltip>
              </span>
            </div>
          </vue-seamless-scroll>
        </div>
      </div>
       
    </div>
    <div class="noData" v-else>
      <div class="no-data-img">
        <img src="../../../../assets/images/planeNoData.png" alt="" />
      </div>
    </div>
     
  </div>
</template>

<script>
import { getSupplyCatalogueList } from "@/api/screen/workhousePage.js";
import vueSeamlessScroll from "vue-seamless-scroll";
export default {
  name: "productionCatalogueTable",
  props: ["height", "width"],
  inject: ["prePageInfo"],
  components: {
    vueSeamlessScroll
  },
  data() {
    return {
      tableData: []
    };
  },
  // 监听属性 类似于data概念
  computed: {
    defaultOption() {
      return {
        step: 0.4, // 数值越大速度滚动越快
        limitMoveNum: 16, // 开始无缝滚动的数据量 this.dataList.length
        hoverStop: true, // 是否开启鼠标悬停stop
        direction: 1, // 0向下 1向上 2向左 3向右
        openWatch: true, // 开启数据实时监控刷新dom
        singleHeight: 0, // 单步运动停止的高度(默认值0是无缝不停止的滚动) direction => 0/1
        singleWidth: 0, // 单步运动停止的宽度(默认值0是无缝不停止的滚动) direction => 2/3
        waitTime: 1000, // 单步运动停止的时间(默认值1000ms)
        warehouseName: null
      };
    },
  },
  mounted() {
    if (this.prePageInfo.queryInfo && this.prePageInfo.queryInfo.name) {
      this.warehouseName = this.prePageInfo.queryInfo.name;
    }
    this.catalogueList();
  },
  methods: {
    catalogueList() {
      getSupplyCatalogueList({
        factoryName: this.warehouseName,
      }).then(res => {
        if(res.code == 200) {
          this.tableData = res.rows;
        } else {
          this.$modal.msgError(res.msg);
        }
      }).catch(err => {
        console.log(err);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import url("../../css/noData.scss");
.productionCatalogueTable {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  position: relative;
  .missMaterialsTable{
    height: 100%;
    width: 100%;
  }
  .table {
    position: relative;
    display: flex;
    flex-direction: column;
    font-size: 1.4rem;
    .line_th {
      height: 4rem;
      display: flex;
      align-items: center;
      color: #ffffffcc;
      font-weight: 500;
      background: url("../../../../assets/images/newScreen/storehouse6_thBg@2x.webp")
        no-repeat center center;
      background-size: 100% 100%;
      .th_name1 {
        font-size: 1.3rem;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .line_tr {
      height: 3rem;
      display: flex;
      line-height: 3rem;
      color: #ffffffcc;
      border-top: 0.1rem dotted #506b81;
      background: url("../../../../assets/images/newScreen/storehouse6_trBg@2x.webp")
        no-repeat center center;
      background-size: 100% 100%;
      cursor: pointer;
    }
    .warp {
      overflow: hidden;
    }
    .th_tr {
      display: inline-block;
      text-align: center;
      font-size: 1.2rem;
    }
    .th_name {
      display: inline-block;
      text-align: center;
      font-size: 1.2rem;
      padding-left: 2%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
}
</style>
  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
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事件,可以在滚动时使表头保持在页面顶部。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级无敌小小小白

感谢老板的打赏~^v^~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值