国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

vue loadmore 組件滑動(dòng)加載更多源碼解析

2019-11-19 16:02:42
字體:
供稿:網(wǎng)友

上一篇講到在項(xiàng)目中使用上拉加載更多組件,但是由于實(shí)際項(xiàng)目開發(fā)中由于需求變更或者說在webview中上拉加載有些機(jī)型在上拉時(shí)候會(huì)把webview也一起上拉導(dǎo)致上拉加載不靈敏等問題,我們有時(shí)候也會(huì)換成滑動(dòng)到底部自動(dòng)加載的功能。

既然都是加載更多,很多代碼思想勢(shì)必相似,主要區(qū)別在于上拉和滑動(dòng)到底部這個(gè)操作上,所以,我們需要注意:

上拉加載是point指針touch觸摸事件,現(xiàn)在因?yàn)槭腔瑒?dòng)加載,需要添加scroll事件去監(jiān)聽然后執(zhí)行相應(yīng)回調(diào)

上拉加載主要計(jì)算觸摸滾動(dòng)距離,滑動(dòng)加載主要計(jì)算container底部和視窗上邊緣的距離

事件綁定改成: 

 mounted() {  ???  this.dom.addEventListener('scroll', this.scroll, false)  ??? }, beforeDestroy() {  ???  this.dom.removeEventListener('scroll', this.scroll, false)  ??? },

事件回調(diào)改為:

 /**  * 滾動(dòng)鉤子  */  scroll() {  const viewHeight = global.innerHeight  let parentNode  if (this.container !== global) {   parentNode = this.$el  } else {   parentNode = this.$el.parentNode  }  if (parentNode) {   // 獲取Vue實(shí)例使用的根 DOM 元素相對(duì)于視口的位置   const rect = parentNode.getBoundingClientRect()   // this.distance 離底部多少距離開始加載   // 如果此元素底邊距離視口頂部的距離小于視口高度加上distance之和,就加載下一頁(yè)   if ((rect.bottom <= viewHeight + this.distance) && this.loadable && !this.loading) {   this.load()   }  }  },

源碼如下:

<template> <div class="loadmore" ref="loadmore"> <div class="loadmore__body">  <slot></slot> </div> <div class="loadmore__footer">  <span v-if="loading">  <i class="tc-loading"></i>  <span>正在加載</span>  </span>  <span v-else-if="loadable">加載更多</span>  <span v-else>沒有更多了</span> </div> </div></template><script type="text/babel"> import axios from 'axios' const CancelToken = axios.CancelToken export default { data() {  return {  /**   * 總頁(yè)數(shù)(由服務(wù)端返回)   * @type {number}   */  count: 0,  /**   * 是否正在拖拽中   * @type {boolean}   */  dragging: false,  /**   * 已加載次數(shù)   * @type {number}   */  times: 0,  /**   * 已開始記載   * @type {boolean}   */  started: false,  /**   * 正在加載中   * @type {boolean}   */  loading: false,  dom: null,  } }, props: {  /**  * 初始化后自動(dòng)開始加載數(shù)據(jù)  */  autoload: {  type: Boolean,  default: true,  },  /**  * 離組件最近的可滾動(dòng)父級(jí)元素(用于監(jiān)聽事件及獲取滾動(dòng)條位置)  */  container: {  // Selector or Element  default: () => (global),  },  /**  * Axios請(qǐng)求參數(shù)配置對(duì)象  * {@link https://github.com/mzabriskie/axios#request-config}  */  options: {  type: Object,  default: null,  },  /**  * 起始頁(yè)碼  */  page: {  type: Number,  default: 1,  },  /**  * 每頁(yè)加載數(shù)據(jù)條數(shù)  */  rows: {  type: Number,  default: 10,  },  /**  * 數(shù)據(jù)加載請(qǐng)求地址  */  url: {  type: String,  default: '',  },  /**  * 距離底部多遠(yuǎn)加載  */  distance: {  type: Number,  default: 200,  }, }, computed: {  /**  * 是否可以加載  * @returns {boolean} 是與否  */  loadable() {  return !this.started || (this.page + this.times) <= this.count  }, }, mounted() {  if (this.container !== global) {  this.dom = document.querySelector(this.container)  } else {  this.dom = this.container  }  if (!this.dom) {  return  }  this.dom.addEventListener('scroll', this.scroll, false)  if (this.autoload && !this.loading) {  this.load()  } }, // eslint-disable-next-line beforeDestroy() {  if (this.dom) {  this.dom.removeEventListener('scroll', this.scroll, false)  } }, methods: {  /**  * 滾動(dòng)鉤子  */  scroll() {  const viewHeight = global.innerHeight  let parentNode  if (this.container !== global) {   parentNode = this.$el  } else {   parentNode = this.$el.parentNode  }  if (parentNode) {   const rect = parentNode.getBoundingClientRect()   if ((rect.bottom <= viewHeight + this.distance) && this.loadable && !this.loading) {   this.load()   }  }  },  /**  * 加載一組數(shù)據(jù)的方法  */  load() {  if (this.loading) {   return  }  this.started = true  this.loading = true  const params = {   currentPage: this.page + this.times,   pageSize: this.rows,  }  const options = Object.assign({}, this.options, {   url: this.url,   cancelToken: new CancelToken((cancel) => {   this.cancel = cancel   }),  })  if (String(options.method).toUpperCase() === 'POST') {   options.data = Object.assign({}, options.data, params)  } else {   options.params = Object.assign({}, options.params, params)  }  this.$axios.request(options).then((res) => {   const data = res.result   this.times += 1   this.loading = false   this.count = data.pageCount   this.$emit('success', data.list)   this.$emit('complete')  }).catch((e) => {   this.loading = false   this.$emit('error', e)   this.$emit('complete')  })  },  /**  * 重置加載相關(guān)變量  */  reset() {  this.count = 0  this.times = 0  this.started = false  this.loading = false  },  /**  *重新開始加載  */  restart() {  this.reset()  this.load()  }, }, }</script>

以上所述是小編給大家介紹的vue loadmore 組件滑動(dòng)加載更多源碼解析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沛县| 神木县| 托克逊县| 厦门市| 武清区| 武乡县| 东城区| 青神县| 湟中县| 佛学| 平谷区| 马鞍山市| 梧州市| 常宁市| 壤塘县| 江津市| 石门县| 永丰县| 莫力| 昭觉县| 集安市| 威海市| 二手房| 蒙阴县| 大港区| 荃湾区| 大新县| 克东县| 石景山区| 大悟县| 修武县| 班玛县| 黄浦区| 罗源县| 华池县| 长泰县| 郴州市| 蕲春县| 长泰县| 舒兰市| 仙居县|