vue开发(五)mint ui实现下拉刷新,上拉加载及自定义加载样式

  1. 按需引入
    先安装
npm install mint-ui -S       // -S表示 --save

在文件中:

import Vue from 'vue'
import 'mint-ui/lib/style.css'
import { Loadmore } from 'mint-ui';
Vue.component(Loadmore.name, Loadmore);
<!-- !!!一定要给loadmore外层设置 overflow: scroll-->
<div style="overflow: scroll;">
	<mt-loadmore
		:top-method="loadTop"
		:bottom-method="loadBottom"
		:auto-fill="false"
		:bottom-all-loaded="loadObj.allLoaded"
		@top-status-change="handleTopChange"
		@bottom-status-change="handleBottomChange"
		ref="loadmore">
		<!-- ##content 因为content部分会出现没数据情况,所以需要wrap这层设置高度将内容撑起来-->
	    <ul class="wrap" style="min-height: calc(100vh - 0.93rem - 64px);">
          <li v-for="item in list" class="page-loadmore-listitem">{{ item }}</li>
        </ul>
        <div class="loadBlock" v-show="loadObj.allLoaded">
        	<span class="loadingWord">{{loadObj.loadingWord}}</span>
        </div>
        <!-- 自定义的刷新时样式 -->
	    <div slot="top" class="mint-loadmore-top">
          <span v-show="topStatus !== 'loading'" :class="{ 'is-rotate': topStatus === 'drop' }">↓</span>
          <span v-show="topStatus === 'loading'">
            <mt-spinner type="snake"></mt-spinner>
          </span>
        </div>    
		<!-- 自定义的加载时样式 -->
        <div slot="bottom" class="mint-loadmore-bottom">
          <span v-show="bottomStatus !== 'loading'" :class="{ 'is-rotate': bottomStatus === 'drop' }">↑</span>
          <span v-show="bottomStatus === 'loading'">
            <mt-spinner type="snake"></mt-spinner>
          </span>
        </div>
	 </mt-loadmore>
</div>
  1. js部分
export default {
    data() {
      return {
		loadObj: {
	        isShowOption: false, // 月份选择popup
	        allLoaded: false, // 下拉加载全部加载
	        topStatus: '', // 上拉刷新的状态变量,自定义样式时用来控制显隐
	        bottomStatus: '', // 下拉加载状态变量,自定义样式时用来控制显隐
	        loadingWord: '正在加载',
	    },
        pageBean: {
	        pageSize: 10, // 当前页显示条数
	        totalElements: 4, // 总条数
	        totalPages: 1, // 总页数
	        currentPage: 0 // 当前页
    	},
      }
    },
    methods: {
        // 存储刷新的状态
		handleTopChange (status) {
		  this.loadObj.topStatus = status
		},
		// 存储加载的状态
		handleBottomChange (status) {
		  this.loadObj.bottomStatus = status
		},
		// 下拉刷新
		loadTop () {
		  setTimeout(() => {
		    this.pageBean.currentPage = 1 // 重置页码
		    this.getRankingListData() // 请求接口方法
		    this.$refs.loadmore.onTopLoaded('refresh')
		  }, 1500)
		},
		// 上拉加载
		// 为什么要设置1.5秒延时:loadBottom是用户开始上拉出发,1.5秒是用户开始大约上拉松手的时间,避免一开始拉就请求接口
		loadBottom () {
		  setTimeout(() => {
		    if (this.pageBean.totalPages === this.pageBean.currentPage) {
		      this.loadObj.loadingWord = '没有更多的数据了'
		      this.loadObj.allLoaded = true
		      this.$refs.loadmore.onBottomLoaded()
		      return
		    }
		    this.pageBean.currentPage++ // 下拉一次页数+1
		    let type = this.isSelectMonth ? 'month' : 'year'
		    this.getRankingListData( 'load') // 同一个请求接口方法,传一个标识是用以判断直接赋值还是拼接,拼接可以直接用concat()
		    this.$refs.loadmore.onBottomLoaded() // 结束动画,可以放在接口回调中,实现数据请求回后再结束动画
		  }, 1500)
    	}
    }
 }
    
  1. 上述代码为项目中实际应用到的部分代码,不完整,下边贴的是官方示例源码,可供参考。
<template>
  <div class="page-loadmore">
    <h1 class="page-title">Pull up</h1>
    <p class="page-loadmore-desc">在列表底部, 按住 - 上拉 - 释放可以获取更多数据</p>
    <p class="page-loadmore-desc">translate : {{ translate }}</p>
    <div class="loading-background" :style="{ transform: 'scale3d(' + moveTranslate + ',' + moveTranslate + ',1)' }">
      translateScale : {{ moveTranslate }} 
    </div>

    <div class="page-loadmore-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }">
      <mt-loadmore :top-method="loadTop" @translate-change="translateChange" @top-status-change="handleTopChange"       :bottom-method="loadBottom" @bottom-status-change="handleBottomChange" :bottom-all-loaded="allLoaded" ref="loadmore">
        
        <ul class="page-loadmore-list">
          <li v-for="item in list" class="page-loadmore-listitem">{{ item }}</li>
        </ul>


       <div slot="top" class="mint-loadmore-top">
          <span v-show="topStatus !== 'loading'" :class="{ 'is-rotate': topStatus === 'drop' }">↓</span>
          <span v-show="topStatus === 'loading'">
            <mt-spinner type="snake"></mt-spinner>
          </span>
        </div>    


        <div slot="bottom" class="mint-loadmore-bottom">
          <span v-show="bottomStatus !== 'loading'" :class="{ 'is-rotate': bottomStatus === 'drop' }">↑</span>
          <span v-show="bottomStatus === 'loading'">
            <mt-spinner type="snake"></mt-spinner>
          </span>
        </div>
      </mt-loadmore>




    </div>
  </div>
</template>

<style>
 


.loading-background, .mint-loadmore-top span {
    -webkit-transition: .2s linear;
    transition: .2s linear
}
.mint-loadmore-top span {
    display: inline-block;
    vertical-align: middle
}

.mint-loadmore-top span.is-rotate {
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg)
}

.page-loadmore .mint-spinner {
    display: inline-block;
    vertical-align: middle
}

.page-loadmore-desc {
    text-align: center;
    color: #666;
    padding-bottom: 5px
}

.page-loadmore-desc:last-of-type,
.page-loadmore-listitem {
    border-bottom: 1px solid #eee
}

.page-loadmore-listitem {
    height: 50px;
    line-height: 50px;
    text-align: center
}

.page-loadmore-listitem:first-child {
    border-top: 1px solid #eee
}

.page-loadmore-wrapper {
    overflow: scroll
}

.mint-loadmore-bottom span {
    display: inline-block;
    -webkit-transition: .2s linear;
    transition: .2s linear;
    vertical-align: middle
}

.mint-loadmore-bottom span.is-rotate {
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg)
}

</style>

<script>
import Vue from 'vue'
import 'mint-ui/lib/style.css'
import NbLoading from '@/components/public/NbLoading'
import { Loadmore } from 'mint-ui'
Vue.component(Loadmore.name, Loadmore)
  export default {
    data() {
      return {
        list: [],
        allLoaded: false,
         bottomStatus: '',
        wrapperHeight: 0, topStatus: '',
        //wrapperHeight: 0,
        translate: 0,
        moveTranslate: 0
      };
    },

    methods: {
      handleBottomChange(status) {
        this.bottomStatus = status;
      },
      loadBottom() {
        setTimeout(() => {
          let lastValue = this.list[this.list.length - 1];
          if (lastValue < 40) {
            for (let i = 1; i <= 10; i++) {
              this.list.push(lastValue + i);
            }
          } else {
            this.allLoaded = true;
          }
          this.$refs.loadmore.onBottomLoaded();
        }, 1500);
      },
      handleTopChange(status) {
        this.moveTranslate = 1;
        this.topStatus = status;
      },
      translateChange(translate) {
        const translateNum = +translate;
        this.translate = translateNum.toFixed(2);
        this.moveTranslate = (1 + translateNum / 70).toFixed(2);
      },
      loadTop() {
        setTimeout(() => {
          let firstValue = this.list[0];
          for (let i = 1; i <= 10; i++) {
            this.list.unshift(firstValue - i);
          }
          this.$refs.loadmore.onTopLoaded();
        }, 1500);
      },
	},
    created() {
      for (let i = 1; i <= 20; i++) {
        this.list.push(i);
      }
    },

    mounted() {
      this.wrapperHeight = document.documentElement.clientHeight - this.$refs.wrapper.getBoundingClientRect().top;
    }
  };
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值