原生js触底加载案例

该段代码展示了在Vue.js中使用axios从API获取房间数据并实现无限滚动加载的逻辑。当滚动到页面底部时,通过节流技术延迟发送请求,更新limit值并加载更多数据。同时,绑定了滚动事件监听器和清理函数以防止内存泄漏。
摘要由CSDN通过智能技术生成

 data() {
    return {
      RoomData: [],
      isBool: false,
      limit: 0,
      isLoading: false,
    }
  },
  methods: {
    roomClick(value) {
      location.href = value
    },
    RoomApi() {
      this.limit = this.limit + 30
      if (this.limit >= 210) {
        this.limit = 180
      }
      this.$axios.get(`http://open.douyucdn.cn/api/RoomApi/live`, {
        params: {
          limit: this.limit
        }
      }).then(r => {
        this.RoomData = r.data.data
      })
    },
    // 滚动回调函数
    scrollHande() {
      // 节流
      // 获取内容高度
      var scrollH = document.documentElement.scrollHeight
      // 获取窗口高度
      var innerH = window.innerHeight
      // 获取滚出去的内容高度
      var top = document.body.scrollTop || document.documentElement.scrollTop
      // 当内容还剩余200的高度未滚出的时候发送请求
      console.log(this.limit);
      if (scrollH - top - innerH < 200) {
        // 节流
        if (!this.isLoading) {
          this.isLoading = true;
          setTimeout(() => {
            // 发送请求
            this.RoomApi();
            this.isLoading = false
          }, 500)
        }
      }

    },
    bindScroll() {
      window.addEventListener('scroll', this.scrollHande)
    },
    // 清除scroll事件
    clearScroll() {
      window.removeEventListener('scroll', this.scrollHande)
    }
  },
  created() {
    this.RoomApi()
    this.bindScroll()
  },

  destroyed() {
    this.clearScroll()
  }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值