search
HomeWeb Front-endJS TutorialMore examples of vue2.0 mobile terminal implementing pull-down refresh and pull-up loading

This article mainly introduces examples of how to implement pull-down refresh and pull-up loading on vue2.0 mobile terminal. The content is quite good. I will share it with you now and give it as a reference.

I am building a front-end architecture based on vue2.0 webpack es6 and sorting out some plug-ins. The following is a pull-down update and a pull-up for more. It is very easy to use. I would like to share it with you.

Go directly to the code. If you don’t understand, read it a few times. I will tell you how to use it below.

<template lang="html">
 <p class="yo-scroll"
 :class="{&#39;down&#39;:(state===0),&#39;up&#39;:(state==1),refresh:(state===2),touch:touching}"
 @touchstart="touchStart($event)"
 @touchmove="touchMove($event)"
 @touchend="touchEnd($event)"
 @scroll="(onInfinite || infiniteLoading) ? onScroll($event) : undefined">
  <section class="inner" :style="{ transform: &#39;translate3d(0, &#39; + top + &#39;px, 0)&#39; }">
   <header class="pull-refresh">
    <slot name="pull-refresh">
      <span class="down-tip">下拉更新</span>
      <span class="up-tip">松开更新</span>
      <span class="refresh-tip">更新中</span>
    </slot>
   </header>
   <slot></slot>
   <footer class="load-more">
    <slot name="load-more">
     <span>加载中……</span>
    </slot>
   </footer>
  </section>
 </p>
</template>

<script>
export default {
 props: {
  offset: {
   type: Number,
   default: 40
  },
  enableInfinite: {
   type: Boolean,
   default: true
  },
  enableRefresh: {
   type: Boolean,
   default: true
  },
  onRefresh: {
   type: Function,
   default: undefined,
   required: false
  },
  onInfinite: {
   type: Function,
   default: undefined,
   require: false
  }
 },
 data() {
  return {
   top: 0,
   state: 0,
   startY: 0,
   touching: false,
   infiniteLoading: false
  }
 },
 methods: {
  touchStart(e) {
   this.startY = e.targetTouches[0].pageY
   this.startScroll = this.$el.scrollTop || 0
   this.touching = true
  },
  touchMove(e) {
   if (!this.enableRefresh || this.$el.scrollTop > 0 || !this.touching) {
    return
   }
   let diff = e.targetTouches[0].pageY - this.startY - this.startScroll
   if (diff > 0) e.preventDefault()
   this.top = Math.pow(diff, 0.8) + (this.state === 2 ? this.offset : 0)

   if (this.state === 2) { // in refreshing
    return
   }
   if (this.top >= this.offset) {
    this.state = 1
   } else {
    this.state = 0
   }
  },
  touchEnd(e) {
   if (!this.enableRefresh) return
   this.touching = false
   if (this.state === 2) { // in refreshing
    this.state = 2
    this.top = this.offset
    return
   }
   if (this.top >= this.offset) { // do refresh
    this.refresh()
   } else { // cancel refresh
    this.state = 0
    this.top = 0
   }
  },
  refresh() {
   this.state = 2
   this.top = this.offset
   this.onRefresh(this.refreshDone)
  },
  refreshDone() {
   this.state = 0
   this.top = 0
  },

  infinite() {
   this.infiniteLoading = true
   this.onInfinite(this.infiniteDone)
  },

  infiniteDone() {
   this.infiniteLoading = false
  },

  onScroll(e) {
   if (!this.enableInfinite || this.infiniteLoading) {
    return
   }
   let outerHeight = this.$el.clientHeight
   let innerHeight = this.$el.querySelector(&#39;.inner&#39;).clientHeight
   let scrollTop = this.$el.scrollTop
   let ptrHeight = this.onRefresh ? this.$el.querySelector(&#39;.pull-refresh&#39;).clientHeight : 0
   let infiniteHeight = this.$el.querySelector(&#39;.load-more&#39;).clientHeight
   let bottom = innerHeight - outerHeight - scrollTop - ptrHeight
   if (bottom < infiniteHeight) this.infinite()
  }
 }
}
</script>
<style>
.yo-scroll {
 position: absolute;
 top: 2.5rem;
 right: 0;
 bottom: 0;
 left: 0;
 overflow: auto;
 -webkit-overflow-scrolling: touch;
 background-color: #ddd
}
.yo-scroll .inner {
 position: absolute;
 top: -2rem;
 width: 100%;
 transition-duration: 300ms;
}
.yo-scroll .pull-refresh {
 position: relative;
 left: 0;
 top: 0;
 width: 100%;
 height: 2rem;
 display: flex;
 align-items: center;
 justify-content: center;
}
.yo-scroll.touch .inner {
 transition-duration: 0ms;
}
.yo-scroll.down .down-tip {
 display: block;
}
.yo-scroll.up .up-tip {
 display: block;
}
.yo-scroll.refresh .refresh-tip {
 display: block;
}
.yo-scroll .down-tip,
.yo-scroll .refresh-tip,
.yo-scroll .up-tip {
 display: none;
}
.yo-scroll .load-more {
 height: 3rem;
 display: flex;
 align-items: center;
 justify-content: center;
} 
</style>

Copy the above component, save it as a component with the suffix .vue and put it under your component, and then introduce it to the page. The following is what I quoted. Code on demo

: There are comments inside, please leave me a message if you have any questions!

<template>
 <p>
    <v-scroll :on-refresh="onRefresh" :on-infinite="onInfinite">
    <ul>
     <li v-for="(item,index) in listdata" >{{item.name}}</li>
     <li v-for="(item,index) in downdata" >{{item.name}}</li>
    </ul>
  </v-scroll>
 </p>
</template>
<script>
import Scroll from &#39;./y-scroll/scroll&#39;;

export default{
 data () {
  return {
   counter : 1, //默认已经显示出15条数据 count等于一是让从16条开始加载
   num : 15, // 一次显示多少条
   pageStart : 0, // 开始页数
   pageEnd : 0, // 结束页数
   listdata: [], // 下拉更新数据存放数组
   downdata: [] // 上拉更多的数据存放数组
  }
 },
 mounted : function(){
   this.getList();
 },
 methods: {
  getList(){
    let vm = this;
     vm.$http.get(&#39;https://api.github.com/repos/typecho-fans/plugins/contents/&#39;).then((response) => {
          vm.listdata = response.data.slice(0,15);
         }, (response) => {
          console.log(&#39;error&#39;);
        });
  },
  onRefresh(done) {
       this.getList();
    
       done() // call done
   
  },
  onInfinite(done) {
       let vm = this;
       vm.$http.get(&#39;https://api.github.com/repos/typecho-fans/plugins/contents/&#39;).then((response) => {
         vm.counter++;
         vm.pageEnd = vm.num * vm.counter;
         vm.pageStart = vm.pageEnd - vm.num;
         let arr = response.data;
           let i = vm.pageStart;
           let end = vm.pageEnd;
           for(; i<end; i++){
            let obj ={};
            obj["name"] = arr[i].name;
            vm.downdata.push(obj);
             if((i + 1) >= response.data.length){
             this.$el.querySelector(&#39;.load-more&#39;).style.display = &#39;none&#39;;
             return;
            }
            }
         done() // call done
          }, (response) => {
          console.log(&#39;error&#39;);
        });
      }
 },
 components : {
&#39;v-scroll&#39;: Scroll
 }
}
</script>

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

v-for loading local static image method in vue

Using Vue to dynamically generate form forms introduce

The above is the detailed content of More examples of vue2.0 mobile terminal implementing pull-down refresh and pull-up loading. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
解决Vue移动端多触点问题解决Vue移动端多触点问题Jun 30, 2023 pm 01:06 PM

在移动端开发中,我们经常会遇到多手指触控的问题。当用户在移动设备上使用多个手指滑动或缩放屏幕时,如何准确地识别和响应这些手势是一个重要的开发难题。在Vue开发中,我们可以采取一些措施来解决移动端多手指触控问题。一、使用vue-touch插件vue-touch是一个用于Vue的手势插件,它可以方便地处理移动端的多手指触控事件。我们可以通过npm安装vue-to

Vue开发中如何解决移动端双击放大问题Vue开发中如何解决移动端双击放大问题Jun 29, 2023 am 11:06 AM

随着移动端设备的普及,使用Vue进行移动端开发已经成为了常见的选择。然而,我们在移动端开发过程中经常会面临一个问题,就是双击放大。本文将针对这一问题,探讨在Vue开发中如何解决移动端双击放大的具体方法。移动端双击放大问题的出现,主要是因为移动设备在触摸屏上进行双击操作时,会自动放大网页的缩放比例。对于一般的网页开发来说,这种双击放大通常是有好处的,因为它可以

使用Python和百度地图API实现移动端地图定位功能的方法使用Python和百度地图API实现移动端地图定位功能的方法Jul 29, 2023 pm 11:33 PM

使用Python和百度地图API实现移动端地图定位功能的方法随着移动互联网的发展,地图定位功能在移动端应用中变得越来越常见。Python作为一种流行的编程语言,也可以通过使用百度地图API来实现移动端地图定位功能。下面将介绍使用Python和百度地图API实现地图定位功能的步骤,并提供相应的代码示例。步骤一:申请百度地图API密钥在开始之前,我们首先需要申请

如何处理PHP表单中的移动端和响应式设计如何处理PHP表单中的移动端和响应式设计Aug 10, 2023 am 11:51 AM

如何处理PHP表单中的移动端和响应式设计随着移动设备的普及和使用频率的增加,以及越来越多的用户使用移动设备访问网站,适配移动端成为了一个重要的问题。在处理PHP表单时,我们需要考虑如何实现移动端友好的界面和响应式设计。本文将介绍如何处理PHP表单中的移动端和响应式设计,并提供代码示例。1.使用HTML5的响应式表单HTML5提供了一些新特性,可以方便地实现响

Vue开发:优化移动端手势缩放卡顿问题Vue开发:优化移动端手势缩放卡顿问题Jun 30, 2023 pm 04:33 PM

Vue开发中如何解决移动端手势缩放页面卡顿问题近年来,移动端应用的普及使得手势操作成为用户交互的重要方式。在Vue开发中,实现移动端手势缩放功能往往会遇到页面卡顿的问题。本文将探讨如何解决这一问题,并提供一些优化策略。了解手势缩放原理在解决问题之前,我们首先需要了解手势缩放的原理。手势缩放通过监听触摸事件来实现,当用户用两个手指滑动屏幕时,页面会按照手指的滑

如何使用PHP生成可用于移动端的二维码?如何使用PHP生成可用于移动端的二维码?Aug 26, 2023 pm 02:51 PM

如何使用PHP生成可用于移动端的二维码?随着移动互联网的快速发展,二维码成为了商家推广、支付、活动等方方面面的重要工具。而使用PHP生成可用于移动端的二维码则成为了许多开发人员的需求。在本文中,我们将介绍如何使用PHP生成可用于移动端的二维码,并附上代码示例供参考。首先,我们需要先安装并引入一个PHP库,名为"endroid/qr-code"。这个库提供了一

Vue移动端消除点击穿透问题的解决方案Vue移动端消除点击穿透问题的解决方案Jul 01, 2023 am 08:27 AM

Vue开发中如何解决移动端点击穿透问题移动端上经常会遇到点击穿透的问题,即用户在快速点击元素时,由于点击事件的执行时间较长,下一个元素会被穿透点击。这在开发中会造成一系列的问题,例如多次触发事件、页面跳转错误等。针对这个问题,Vue提供了几种解决方案。一、使用FastClick库FastClick是一个能够消除click事件在移动端300ms的延迟库。安装和

Vue开发中如何解决移动端图片旋转问题Vue开发中如何解决移动端图片旋转问题Jun 29, 2023 pm 09:22 PM

随着移动互联网的快速发展,越来越多的网站和应用程序开始采用Vue.js进行移动端开发。然而,在移动端开发过程中,经常会遇到图片旋转的问题。图片旋转是指当用户在移动设备上拍摄照片时,由于设备方向的变化,导致照片在页面上显示的角度与实际拍摄的角度不一致。解决图片旋转问题,首先需要了解图片旋转的原因。当用户在移动设备上拍摄照片时,设备会自动为照片添加一些元数据,其

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools