H5移动端分页(vue)

该博客主要讨论了前端页面中数据的加载策略,通过监听滚动事件实现分页加载。在`mounted`和`destroyed`钩子中分别添加和移除滚动事件监听,确保性能优化。`handleScroll`函数判断滚动条位置,当接近页面底部时,更新`loadMore`状态并调用`getList`方法加载更多数据。此外,还涉及到了微官网的获取功能。
摘要由CSDN通过智能技术生成
export default {
  data() {
    return {
      dataList: [],
      TotalPage: 1,
      loadMore: false,
    };
  },
  mounted() {
    this.getList(this.TotalPage);
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    getList(page) {
      //请求数据
      getList(page)
        .then((res) => {
          if (res.data.code == 1) {
            if (this.dataList.length == 0) {
              this.loadingFlag = false;
              this.dataList = res.data.data.data;
            } else {
              this.loadingFlag = false;
              this.dataList = this.dataList.concat(res.data.data.data);
            }
            if (res.data.data.data.length > 0) {
              this.$nextTick(() => {
                this.loadMore = true;
              });
            }
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    // 网页滚动条纵坐标的位置
    getScrollTop() {
      let scrollTop = 0,
        bodyScrollTop = 0,
        documentScrollTop = 0;
      if (document.body) {
        bodyScrollTop = document.body.scrollTop;
      }
      if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
      }
      scrollTop =
        bodyScrollTop - documentScrollTop > 0
          ? bodyScrollTop
          : documentScrollTop;
      return scrollTop;
    }, 
    // 网页的总高度
    getScrollHeight() {
      let scrollHeight = 0,
        bodyScrollHeight = 0,
        documentScrollHeight = 0;
      if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
      }
      if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
      }
      scrollHeight =
        bodyScrollHeight - documentScrollHeight > 0
          ? bodyScrollHeight
          : documentScrollHeight;
      return scrollHeight;
    },
    handleScroll: function () {
      const t = this;
      let getScrollTop = this.getScrollTop();
      let getWindowHeight = this.getWindowHeight();
      let getScrollHeight = this.getScrollHeight();
      if (
        getScrollTop + getWindowHeight >= getScrollHeight - 50 &&
        this.loadMore
      ) {
        this.loadMore = false;
        t.TotalPage++;
        t.getList(t.TotalPage);
      }
    }, 
    // 网页可见区域高
    getWindowHeight() {
      let windowHeight = 0;
      if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
      } else {
        windowHeight = document.body.clientHeight;
      }
      return windowHeight;
    },
    //微官网
    websiteFn() {
      let orgUuid = localStorage.getItem("orgUuid");
      getWebsite(orgUuid)
        .then((res) => {
          console.log(res);
          if (res.data.code == 1) {
            // Toast(res.data.msg);
            this.show = true;
            this.miniPro = res.data.data;
          } else {
            Toast(res.data.msg);
          }
        })
        .catch((e) => {});
    },
  },
};
Vue是一种用于构建用户界面的JavaScript框架,适用于各种平台,包括H5移动端。使用Vue来写H5移动端应用具有以下几个优势。 首先,Vue提供了丰富的组件库和生态系统,方便我们快速构建出符合H5移动端需求的用户界面。我们可以使用Vue的组件来封装各种常见的移动端UI元素,例如按钮、弹窗、导航栏等,以减少我们的开发时间和工作量。 其次,Vue具有响应式的数据绑定机制,能够方便地将数据和视图进行关联。在H5移动端应用中,我们常常需要根据用户的操作改变界面的显示和行为,通过Vue的数据绑定,我们可以很容易地实现这一需求,从而提升用户体验。 此外,Vue还支持组件间的通信和交互。在H5移动端应用中,各个组件之间经常需要进行数据传递和事件触发,Vue提供了一系列的机制,例如props和emit,来实现组件间的数据传递和通信,从而使得我们的应用更加灵活和可维护。 最后,Vue还有一些针对H5移动端的特性和优化。例如,Vue可以通过Webpack等打包工具将代码进行优化和压缩,减小文件大小,提高页面加载速度;另外,Vue还支持使用Vue Router进行页面路由管理,使用Vuex进行全局状态管理,来满足复杂H5应用的需求。 综上所述,Vue是一种适用于H5移动端的强大框架,它能够帮助我们快速构建出漂亮、高效的移动端应用,提升用户体验,同时还具备灵活性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值