Pull-down refresh and pull-up loading components based on Vue encapsulation

Pull-down refresh and pull-up loading components based on Vue encapsulation

Based on Vue and native javascript encapsulation, pull-down refresh and pull-up loading components are provided for your reference. The specific contents are as follows

  • The upTilte slot is where the custom content of the pull-down refresh is placed
  • The downTilte slot is where the custom content loaded from the pull-up is placed
  • The default slot is the list content area

The component code is as follows

<template>
  <div class="refresh" id="refresh">
    <slot name="upTilte"></slot>
    <slot></slot>
    <slot name="downTilte"></slot>
  </div>
</template>

<script>
export default {
  name: 'PullupOrPulldownRefresh',
  props: {
    // Maximum moving distance maxMove: {
      type: Number,
      default: 300
    },
    // Damping coefficient friction: {
      type: Number,
      default: 0.3
    }
  },
  data() {
    return {
      startY: 0,
      ul: null,
      draw: null,
      up: null,
      down: null,
      y: 0 // Inertial rebound distance}
  },
  mounted() {
    this.$nextTick(() => {
      this.draw = document.getElementById('refresh')
      this.ul = this.draw.children[1]
      this.up = this.draw.children[0]
      this.down = this.draw.children[2]
      this.draw.addEventListener('touchstart', this.touchstart)
      this.draw.addEventListener('touchmove', this.touchmoveEvent)
      this.draw.addEventListener('touchend', this.touchendEvent)
    })
  },
  methods: {
    // Touch start event touchstart(event) {
      this.startY = event.changedTouches[0].clientY
    },
    // Touch move event touchmoveEvent(event) {
      const height = this.ul.clientHeight - this.draw.clientHeight
      if (height === this.draw.scrollTop || this.draw.scrollTop === 0) {
        var a = event.changedTouches[0].clientY - this.startY
        this.y = a <= this.maxMove ? a : this.maxMove
        // In order to eliminate the lag problem, you need to clear the transition effect this.ul.style.transition = 'none'
        this.ul.style.transform = 'translateY(' + this.friction * this.y + 'px)'
        // Modify status const upHeight = -this.up.clientHeight + this.friction * this.y
        // Pull down to start if (this.friction * this.y > 0) (this.setStatus(this.friction * this.y), this.up.style.transition = 'none', this.up.style.transform = 'translateY(' + upHeight + 'px) translateX(-50%)')
        // Pull up and start if (this.friction * this.y < 0) (this.setStatus(this.friction * this.y), this.down.style.transition = 'none', this.down.style.marginTop = this.friction * this.y + 'px')
      }
    },
    // Touch end event touchendEvent(event) {
      if (this.friction * this.y >= 50) this.$emit('RefreshUp', this.friction * this.y)
      else if (this.friction * this.y < -50) this.$emit('RefreshDown', this.friction * this.y)
      else this.resetStyle()
    },
    // Reset and add transition effects resetStyle() {
      this.ul.style.transition = 'transform .6s'
      this.ul.style.transform = 'translateY(' + 0 + 'px)'
      this.up.style.transition = 'all .6s'
      this.up.style.transform = 'translateY(-' + this.up.clientHeight + 'px) translateX(-50%)'
      this.down.style.transition = 'all .6s'
      this.down.style.marginTop = -this.down.clientHeight + 'px'
    },
    // Set the refresh status setStatus(y) {
      this.$emit('setStatus', y)
    }
  }
}
</script>

<style lang="scss">
.refresh {
  width: 100%;
  height: 100vh;
  border: 2px solid #ccc;
  position: relative;
  overflow: hidden;
  overflow:auto;
  position: fixed;
  ul {
    zoom: 1;
    padding: 0 10%;
  }

  ul::after {
    content: '';
    display: block;
    visibility: hidden;
    height: 0;
    clear: both;
  }

  li {
    list-style: none;
    width: 100%;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
  .UpRefresh {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    z-index: -9;
  }
  .DownRefresh {
    position: relative;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -10px;
    z-index: -9;
  }
}
</style>
  • How to use components
  • friction is the friction coefficient
  • @RefreshUp triggers the event when the user pulls down to a certain distance
  • @RefreshDown triggers the event when the device is pulled up to a certain distance
  • @setStatus is a method to change the refresh status
<template>
  <div>
    <PullupOrPulldownRefresh
      ref="PullupOrPulldownRefresh"
      :maxMove="maxMove"
      :friction="friction"
      @RefreshUp="RefreshUp"
      @RefreshDown="RefreshDown"
      @setStatus="setStatus"
    >
      <template v-slot:upTilte>
        <!-- <div class="UpRefresh" v-show="isUpRefresh">{{ Uptitle }}</div> -->
        <div class="UpRefresh" v-show="isUpRefresh">
          <img :src="require('@/assets/logo.png')" alt="" />
          <p>{{ Uptitle }}</p>
        </div>
      </template>
      <ul>
        <li
          v-for="(item, index) in data"
          :key="index"
          style="background: orange"
        >
          {{ item }}
        </li>
      </ul>
      <template v-slot:downTilte>
        <div class="DownRefresh" v-show="isDownRefresh">{{ Downtitle }}</div>
      </template>
    </PullupOrPulldownRefresh>
  </div>
</template>

<script>
export default {
  data() {
    return {
      maxMove: 300,
      friction: 0.3,
      data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      isUpRefresh: false,
      isDownRefresh: false,
      Downtitle: 'Pull up to load more',
      Uptitle: 'Pull down to refresh'
    }
  },
  methods: {
    setStatus(y) {
      if (y && y > 0) {
        this.isUpRefresh = true
        this.Uptitle = 'Pull down to refresh'
        if (y >= 50) this.Uptitle = 'Release to refresh'
        return
      }
      this.isDownRefresh = true
      this.Downtitle = 'Pull up to load more'
      if (y <= -50) this.Downtitle = 'Release to load more'
    },
    RefreshUp(y) {
      if (!y) return
      if (y >= 50) {
        this.Uptitle = 'Refreshing'
        setTimeout(() => {
          for (var i = 1; i <= 10; i++) {
            this.data.push(this.data[this.data.length - 1] + 1)
          }
          this.$refs.PullupOrPulldownRefresh.resetStyle() // Rebound reset}, 1000)
      }
    },
    RefreshDown(y) {
      if (!y) return
      if (y <= -50) {
        this.Downtitle = 'Loading'
        setTimeout(() => {
          for (var i = 1; i <= 10; i++) {
            this.data.push(this.data[this.data.length - 1] + 1)
          }
          this.$refs.PullupOrPulldownRefresh.resetStyle() // Rebound reset}, 1000)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.UpRefresh img{
  width: 30px;
}
</style>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the pitfalls of using the Vant framework to do H5 (pull down to refresh, pull up to load, etc.)
  • Vue sample code for pull-up loading and pull-down refreshing based on vant
  • Usage of van-list in vant
  • A brief discussion on the problem of pull-up loading and pull-down refreshing in Vant-list

<<:  Example verification MySQL | update field with the same value will record binlog

>>:  How to deploy Vue project using Docker image + nginx

Recommend

Docker builds jenkins+maven code building and deployment platform

Table of contents Docker Basic Concepts Docker in...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

An article tells you how to implement Vue front-end paging and back-end paging

Table of contents 1: Front-end handwritten paging...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

Add crontab scheduled tasks to debian docker container

Now most of the Docker images are based on Debian...

MySQL log trigger implementation code

SQL statement DROP TRIGGER IF EXISTS sys_menu_edi...

Complete list of CentOS7 firewall operation commands

Table of contents Install: 1. Basic use of firewa...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...

HTML Tutorial: Definition List

<br />Original text: http://andymao.com/andy...

WeChat applet canvas implements signature function

In the WeChat applet project, the development mod...

5 Commands to Use the Calculator in Linux Command Line

Hello everyone, I am Liang Xu. When using Linux, ...

Summary of Docker Data Storage

Before reading this article, I hope you have a ba...

Raspberry Pi msmtp and mutt installation and configuration tutorial

1. Install mutt sudo apt-get install mutt 2. Inst...