


An article explains in detail how vue2 implements the pull-down loading function with damping
This article brings you relevant knowledge about vue2. It mainly talks about how the function of damped pull-down loading is implemented in vue2. Interested friends, let’s take a look. I hope everyone has to help.
In vue, you need to bind the triggered event
<div id="testchatBox" class="chatWrap" :style="{paddingTop: chatScroollTop + 'px'}" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"> </div>
The code snippet uses three callback functions:
-
touchstart: The moment when the finger touches the screen
touchmove: The moment when the finger moves on the screen
touchend: The finger When leaving the screen
As you can see from paddingTop, we achieve the drop-down effect by controlling the padding at the top of the container distance. So our re-adjustment is to determine the value of chatScrollTop through the three callback functions above.
You can know from the name chatScrollTop that our pull-down refresh is used in the chat box container.
We need to use these variables:
data() { return { chatScroollTop: 0, // 容器距离顶部的距离 isMove: false, // 是否处于touchmove状态 startY: 0, // 当前手指在屏幕中的y轴值 pageScrollTop: 0, // 滚动条当前的纵坐标 } }
The three callback functions correspond Three stages, and our core code is also divided into three parts:
The first part: initialize the distance from the top of the current container, and initialize whether it is currently in a sliding state, and obtain the ordinate of the current scroll bar .
touchStart(e) { // e代表该事件对象,e.targetTouches[0].pageY可以拿到手指按下的 y轴点 this.startY = e.targetTouches[0].pageY // 开启下拉刷新状态 this.isMove = false this.pageScrollTop = document.documentElement && document.documentElement.scrollTop }
Part 2: Determine the distance between the container and the top based on the ordinate difference between the current finger's current distance and the moment it touches the screen. But since it can't slide all the time, it gives a 0 -> 80 atmosphere. In order to make sliding more interesting, a step value is added to adjust the sliding distance ratio. The so-called distance ratio means that the farther the finger is from the beginning, the shorter the distance the capacity will follow to slide. Achieve a damping-like effect.
touchMove(e) { // 这个 touchMove,只要页面在动都会发生的,所以 touching就起作用了 // 获取移动的距离 let diff = e.targetTouches[0].pageY - this.startY let step = 60 if (diff > 0 && diff < 80 && this.pageScrollTop === 0) { step++ // 越来越大 this.chatScroollTop += (diff / (step * 0.1)) // 越向下给人的阻力感越大 this.isMove = true } }
Part 3: After releasing your finger, give a distance from the top to add a loading scroll bar.
touchEnd() { if(this.isMove) { this.chatScroollTop = 40 this.downCallback() // api拉取数据 } } async downCallback() { try { // 拿数据 } catch() {} finall{ this.chatScrollTop = 0 } }
Recommended learning: "vue.js video tutorial"
The above is the detailed content of An article explains in detail how vue2 implements the pull-down loading function with damping. For more information, please follow other related articles on the PHP Chinese website!

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.

Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.

Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.

Vue.js and React each have their own advantages and disadvantages. When choosing, you need to comprehensively consider team skills, project size and performance requirements. 1) Vue.js is suitable for fast development and small projects, with a low learning curve, but deep nested objects can cause performance problems. 2) React is suitable for large and complex applications, with a rich ecosystem, but frequent updates may lead to performance bottlenecks.

Vue.js is suitable for small to medium-sized projects, while React is suitable for large projects and complex application scenarios. 1) Vue.js is easy to use and is suitable for rapid prototyping and small applications. 2) React has more advantages in handling complex state management and performance optimization, and is suitable for large projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use
