zoukankan      html  css  js  c++  java
  • vue 上拉加载自定义组件,超好用哦

    1.创建组件components > zj-roll > index.vue

    <template>
      <div>
        <slot></slot>
        <div class='bottom' v-if='(!lastPage.total && lastPage.empty!=0)'>---------- <span >我也是有底线的</span> ----------</div>
        <div class='loading'  v-if='lastPage.total && lastPage.empty!=0'>
          <!-- <img src='@/assets/loading.png' /> -->
          玩命加载中</div>
      </div>
    </template>
    <script>
    export default {
      name: 'scroll',
      props: ['lastPage'],
      data: function () {
        return {
          startY: 0,
          endY: 0,
          initTop: null,
          move: 0,
          a: 0,
          b: 0
        }
      },
      methods: {
        getScrollTop () {
          let scrollTop = 0
          let bodyScrollTop = 0
          let 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
          let bodyScrollHeight = 0
          let documentScrollHeight = 0
          if (document.body) {
            bodyScrollHeight = document.body.scrollHeight
          }
          if (document.documentElement) {
            documentScrollHeight = document.documentElement.scrollHeight
          }
          scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
          return scrollHeight
        },
    // 浏览器视口的高度
        getWindowHeight () {
          var windowHeight = 0
          if (document.compatMode === 'CSS1Compat') {
            windowHeight = document.documentElement.clientHeight
          } else {
            windowHeight = document.body.clientHeight
          }
          return windowHeight
        },
        handleScroll () {
          if (this.getScrollTop() + this.getWindowHeight() === this.getScrollHeight()) {
            this.$emit('zj-load')
          }
        }
      },
      mounted () {
        window.addEventListener('scroll', this.handleScroll)
      }
    }
    </script>
    <style lang="less" scoped>
    .bottom{
      padding:20px 0;
      width:100%;
      text-align: center;
      font-size:16px;
      color: #bbb;
      span{
        margin:0 16px;
      }
    }
    .loading{
      padding:20px 0;
      text-align: center;
      display: flex;
      justify-content:center;
      align-items: center;
      font-size:16px;
      color: #bbb;
      img{
        animation: mymove .5s linear infinite;
        width:30px;
        height:30px;
        margin-right:10px;
      }
    }
    
    @-webkit-keyframes mymove {
      0%   {transform:rotate(0deg);}
      50%  {transform:rotate(180deg);}
      100% {transform:rotate(360deg);}
    }
    
    </style>

    2.页面中引用组件

    <template>
      <div class="collection" v-if="show">
        <scroll style="position:relative;-webkit-overflow-scrolling : touch;overflow:auto;margin-bottom:45px;" @zj-load="listLoad" :lastPage='{total: !allLoaded, empty: ulList.length}'>         
          <x-header :left-options="{backText: ''}">我的收藏</x-header>
    
          <stopData :ulList="ulList"  collection="收藏"  v-if="ulList.length > 0"></stopData>
    
          <div class="noPoster" v-else>
            <div class="font32">
              <img src="../../assets/sp_iconx.png"/>暂无收藏~
            </div>
          </div>
        </scroll>
      </div>
    </template>
    
    <script>
    import { XHeader } from 'vux'
    import stopData from '@/components/stop'
    import { collectGoods } from '@/api'
    import { timingSafeEqual } from 'crypto';
    import { types } from 'util';
    import scroll from '@/components/zj-roll'
    
    export default {
      data () {
        return {
          ulList: [],
          pageSize: 3,
          pageIndex: 1,
          show: false,
          allLoaded: false
        }
      },
      components: {
        stopData,
        XHeader,
        scroll
      },
      mounted () {
        this.defined()
      },
      methods: {
        listLoad(){
          let that = this
          if (!that.allLoaded) {
            that.defined()       
          }
        },
        async defined(){
          let that = this
        //调取收藏列表数据 const {data} = await collectGoods({pageSize: this.pageSize, pageIndex: this.pageIndex}) if (data.code == 0) { this.ulList = this.ulList.concat(data.data.page.list)
         // 判断是否还有下一页,如果是pageIndex加一
    if (data.data.page.hasNextPage) { this.allLoaded = false this.pageIndex = this.pageIndex + 1 } else { this.allLoaded = true } this.show = true } }, } } </script> <style lang="less"> .collection{ .noPoster{ margin-top: 5.333333rem /* 400/75 */; display: flex; justify-content: center; align-items: center; color: #999999; z-index: 0; background: #ffffff; div{ display: flex; align-items: center; } img{ width: .746667rem /* 56/75 */; height: .8rem /* 60/75 */; margin-right: .32rem /* 24/75 */; } } } </style>
  • 相关阅读:
    使用CSS3的@media来编写响应式的页面
    转帖 移动端h5页面不同尺寸屏幕适配兼容方法
    转 关于HTML5中meta name="viewport" 的用法 不同分辨率手机比例缩放
    转帖 移动前端开发之viewport的深入理解
    Python学习---字符串操作
    Python学习---基础篇
    Qt托盘程序
    MySQL常用语句
    C++模式学习------适配器模式
    C++模式学习------原型模式
  • 原文地址:https://www.cnblogs.com/gqx-html/p/9962412.html
Copyright © 2011-2022 走看看