uniapp实现虚拟滚动

<scroll-view
      scroll-y="true"
      class="scroll-Y"
      lower-threshold="300"
      :style="{ height: testData.length * 40 }"
      @scrolltolower="lower"
      @scroll="scroll"
    > 
      <view class="parentDom">
        <view :style="{ height: testData.length * 40 + 'px' }"></view>
        <view class="positionRelative" :style="{ top: scrollTData + 'px' }">
          <view v-for="item in visibleData" :key="item.id" class="height40" @click="choseSick(item)">{{item.name}}</view>
        </view>
      </view> 
    </scroll-view>
props: {
    itemSize: {
      type: Number,
      default: 40
    }
  },
  data() {
    return {
      searchList: [],
      screenHeight: 2000, //可视高度,到时候换成自己的,每条数据行高*需要展示的条数
      startOffset: 0,
      start: 0,
      end: 50,//没屏展示多少条数据,和上面screenHeight有关系
      scrollTData: 0
      // testData: []
    };
  },
  computed: {
    listHeight() {
      return this.listData.length * this.itemSize;
    },
    visibleCount() {
      return Math.ceil(this.screenHeight / this.itemSize);
    },
    getTransform() {
      return `translate3d(0,${this.startOffset}px,0)`;
    },
    visibleData() { //可视区域截取相关数据
      return this.testData.slice(this.start, Math.min(this.end, this.testData.length));
    },
    testData() {
      let data = [];
      for (let i = 0; i < 500; i++) {
        data.push({ id: i, name: '疾病' + i });
      }
      console.log('疾病', data);
      return data;
    }
  },
  methods:{
    scroll(e) {
      console.log(2, e);
      let scrollTop = e.target.scrollTop;
      // 此时的开始索引
      this.start = Math.floor(scrollTop / this.itemSize);
      // 此时的结束索引
      this.end = this.start + this.visibleCount;
      // 此时的偏移量
      console.log('位置', this.start, this.end);
      this.startOffset = scrollTop - (scrollTop % this.itemSize);
    },
  }
.scroll-Y {
  height: 100%;
  overflow-y: scroll;
}
.height40 {
  height: 40px;
}
.positionRelative {
  height: 100%;
  overflow-y: scroll;
  position: absolute;
  left: 0;
  top: 0;
  //   height: 100%;
  //   width: 100%;
}
.parentDom {
  position: relative;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值