首页 > 其他分享 >vue无限滚动加载

vue无限滚动加载

时间:2022-11-25 10:00:34浏览次数:61  
标签:vue throttle const list page 滚动 data menuList 加载

vue无限滚动加载

1、使用防抖完成无限加载

export default {
  data() {
    return {
      menuList: {},
      time: null,
      page: 1,
      loading: true,
    };
  },
  methods: {
    windowScroll() {
      this.time = null;
      window.addEventListener("scroll", () => {
        if (this.time) {
          clearTimeout(this.time);
        }
        this.time = setTimeout(() => {
          const window_height = document.body.scrollHeight; //滚动条总高度
          const scrollTop = document.documentElement.scrollTop; //滚动条距离顶部距离
          const clientHeight = document.documentElement.clientHeight; //页面可视区高度
          if (scrollTop + clientHeight >= window_height - 20) {
            menuPush();
          }
        }, 300);
      });
      const menuPush = async () => {
        if (this.page < this.menuList.total / this.menuList.page_size) {
          this.loading = true;
          this.page = this.page + 1;
          const list = (await getMenuQuery({ page: this.page })).data.list;
          this.menuList.list = this.menuList.list.concat(list);
          this.loading = false;
        }
      };
    },
  },
  async mounted() {
    this.windowScroll();
    this.menuList = (await getMenuQuery({ page: this.page })).data;
    this.loading = false;
  },
};

2、使用throttle-debounce

  1. 下载 throttle-debounce npm i throttle-debounce
  2. 引用 import { throttle } from "throttle-debounce";
//引入节流
import { throttle } from "throttle-debounce";
export default {
  data() {
    return {
      menuList: [],
      isloading: false,
      page: 1,
    };
  },
  //挂载完成
  async mounted() {
    //获取菜单数据
    const menuList = await getMenu({ page: this.page });
    this.menuList = menuList.data.list;

    //throttle() 返回值是一个节流函数
    this.throttle = throttle(1000, this.scroll.bind(this));
    // 绑定滚动事件
    window.addEventListener("scroll", this.throttle);
  },
  methods: {
    //滚动
    scroll() {
      //正在加载时,不再重复调用数据
      if (this.isloading) {
        return;
      }
      //getBoundingClientRect()
      // 获取浏览器内部窗口的高度
      const h = window.document.documentElement.clientHeight;
      // 获取列表底部距视口顶部的距离
      const l = this.$refs.list.$el.getBoundingClientRect().bottom;
      if (h > l) {
        // 触底
        this.isloading = true;
        //获取下一页
        this.page++;
        // 加载数据,获取第二页的内容
        //防抖和节流
        getMenu({ page: this.page }).then((res) => {
          //隐藏提示
          this.isloading = false;
          //将数据放到 menuList 中
          //...展开运算符
          this.menuList.push(...res.data.list);
        });
      }
    },
  },
};

https://blog.csdn.net/u011818572/article/details/79117861

 

标签:vue,throttle,const,list,page,滚动,data,menuList,加载
From: https://www.cnblogs.com/Lmyong/p/16924247.html

相关文章

  • vue中引入字体
    前言:做大屏项目需要引入字体做个记录一、先看看效果   二、实现1、下载字体文件分享一个下载开源字体网站:https://www.dafont.com/theme.php2、文件放到项目中可......
  • vue获取内网ip
    vue获取内网ip函数getIPs1(callback){varip_dups={};//compatibilityforfirefoxandchromevarRTCPeerConnection=window.RTCPeerConnection|......
  • Vue3学习(九)
    路由学习:1:路由传参<template><divclass="cls">这是电影<h2>{{$route.params.id}}</h2><h2>{{$route.params.type}}-{{$route.params}}<......
  • vue3中watch监听不是你想的那样简单
    vue3中watch监听数组,数组变化后未触发回调今天发生了一个很神奇的现象,就是我使用watch监听数组时。被监听的数组已经发生了变化。但是没有触发回调操作。当时的我感到......
  • Vue -- Mixin混入(二)
    前言使用Mixin混入自定义属性自定义属性:就是直接写在组件里的属性定义一个Mixin,并写入自定义属性 constmyMixin={num:1};创建vue实例,定义......
  • Vue项目打包后部署到express服务器
    背景有的时候我们在使用vue脚手架开发完项目后想在本地的服务器上运行进行调试,因为有的时候开发时和打包后的效果有些许差异。步骤安装expressnpminstallexpress-sa......
  • Vue脚手架,从入门到放弃
    创建Vue脚手架Vue脚手架是官方提供的标准化开发工具(最新4.x版本)网址:https://cli.vuejs.org/zh/guide/安装使用淘宝镜像源安装npminstall--location=globalcnp......
  • Go加载依赖包命令
     gopath环境变量修改有时默认gopath可能在C盘,重装系统后,下载的包会丢失,这时我们执行命令,修改到其它盘即可setgopath=D:\code\go创建项目时,最好将项目放......
  • vue打包后405
    proxy只是解决了开发环境的跨域,线上依然会产生跨域的问题。上线时需要配置nginx!location^~/prod-api/{rewrite^/prod-api/(.*)$/$1b......
  • Vue
    前提Vue进度:https://www.bilibili.com/video/BV1Zy4y1K7SH/?p=6&spm_id_from=pageDriver&vd_source=81bee25537470aeb5c65db3a5bba55ac进度:第23节Vue2文档教程:......