Vue imitates QQ left slide to delete component function
When a friend was developing a vue project a few days ago, someone reported that there was a problem with the sliding click on IOS. Let us help solve it, so I rewrote the code. Below, I will share the vue imitation qq left-swipe deletion component function to Script House platform, friends who need it can refer to it
A few days ago, when I was developing the Vue project, because the code written by others had a small bug, someone reported that there was something wrong with the sliding click on IOS, so I asked I'm here to help solve it. I looked at the previous code implementation and it was cumbersome and redundant, so I simply re-wrote it myself for your reference. If there is a better way, please feel free to communicate in time~
Let’s take a look at the renderings first. After all, there is no truth without pictures~
Renderings
Implementation ideas
The specific implementation ideas are as follows:
In terms of layout, I use rem flex layout. For the specific structure and style, please refer to my Code, it is worth noting that the delete button at the end is placed at the end of each row through positioning, and it is hidden beyond the scope.
The left and right swipes are through touchstart and touchend events. By judging the start and end of the sliding, the offset in the horizontal direction x, if it is greater than a certain threshold, it is considered a left sliding, and if it is less than a certain threshold, it is considered a right sliding. The sliding is changed through the translate offset of the parent li element. My implementation method here is to declare the style in advance and switch the style by changing the type value of the current parent li
When clicking a certain slider, first determine whether all the current sliders are in the slide-out state. If so, you must first restore the status of all sliders. If not, the click will take effect. Here I am It just pops up an alter. The specific business can be filled in according to the actual situation.
Deletion is relatively simple. When the slider is drawn, a delete button appears. Click the button to get the current array index value. Through For the splice method of the array, just delete the corresponding array value
- Specific implementation
##Html code
<p class="container"> <p class="page-title">滑动组件</p> <ul> <li class="list-item " v-for="(item,index) in list " data-type="0"> <p class="list-box" @touchstart.capture="touchStart" @touchend.capture="touchEnd" @click="skip"> <img class="list-img lazy" src="/static/imghwm/default1.png" data-src="item.imgUrl" : alt=""> <p class="list-content"> <p class="title">{{item.title}}</p> <p class="tips">{{item.tips}}</p> <p class="time">{{item.time}}</p> </p> </p> <p class="delete" @click="deleteItem" :data-index="index">删除</p> </li> </ul> </p>
Note: The data here are all local mock~
Css style Code
.page-title{ text-align: center; font-size: 17px; padding: 10px 15px; position: relative; } .page-title:after{ content: " "; position: absolute; left: 0; bottom: 0; right: 0; height: 1px; border-bottom: 1px solid #ccc; color: #ccc; -webkit-transform-origin: 0 100%; transform-origin: 0 100%; -webkit-transform: scaleY(0.5); transform: scaleY(0.5); z-index: 2; } .list-item{ position: relative; height: 1.6rem; -webkit-transition: all 0.2s; transition: all 0.2s; } .list-item[data-type="0"]{ transform: translate3d(0,0,0); } .list-item[data-type="1"]{ transform: translate3d(-2rem,0,0); } .list-item:after{ content: " "; position: absolute; left: 0.2rem; bottom: 0; right: 0; height: 1px; border-bottom: 1px solid #ccc; color: #ccc; -webkit-transform-origin: 0 100%; transform-origin: 0 100%; -webkit-transform: scaleY(0.5); transform: scaleY(0.5); z-index: 2; } .list-box{ padding: 0.2rem; background: #fff; display: flex; align-items: center; -webkit-box-sizing: border-box; box-sizing: border-box; justify-content: flex-end; position: absolute; top: 0; right: 0; bottom: 0; left: 0; font-size: 0; } .list-item .list-img{ display: block; width: 1rem; height: 1rem; } .list-item .list-content{ padding: 0.1rem 0 0.1rem 0.2rem; position: relative; flex: 1; flex-direction: column; align-items: flex-start; justify-content: center; overflow: hidden; } .list-item .title{ display: block; color: #333; overflow: hidden; font-size: 15px; font-weight: bold; text-overflow: ellipsis; white-space: nowrap; } .list-item .tips{ display: block; overflow: hidden; font-size: 12px; color: #999; line-height: 20px; text-overflow: ellipsis; white-space: nowrap; } .list-item .time{ display: block; font-size: 12px; position: absolute; right: 0; top: 0.1rem; color: #666; } .list-item .delete{ width: 2rem; height: 1.6rem; background: #ff4949; font-size: 17px; color: #fff; text-align: center; line-height: 1.6rem; position: absolute; top:0; right: -2rem; }This is the core style code, and some style reset code is placed in App.vue, by calculating the root node html I put the font size script in index.html~js code
export default{ name: 'index', data () { return { list : [ { title : 'Chrome更新了' , imgUrl : './static/images/Chrome.png' , tips : '不再支持Flash视频播放' , time : '上午 8:30' }, { title : '电影新资讯' , imgUrl : './static/images/Sina.png' , tips : '电影《红海行动》上映以来票房暴涨,很多国人表示对国产电影有了新的改观' , time : '上午 12:00' }, { title : '聚焦两会·共筑中国梦' , imgUrl : './static/images/video.png' , tips : '习近平代表的两会故事' , time : '下午 17:45' }, { title : '微信团队' , imgUrl : './static/images/Wechat.png' , tips : '您的帐号有异常登录,如非本人操作,请及时修改密码' , time : '昨天' } ], startX : 0 , endX : 0 , } }, methods : { //跳转 skip(){ if( this.checkSlide() ){ this.restSlide(); }else{ alert('You click the slide!') } }, //滑动开始 touchStart(e){ // 记录初始位置 this.startX = e.touches[0].clientX; }, //滑动结束 touchEnd(e){ // 当前滑动的父级元素 let parentElement = e.currentTarget.parentElement; // 记录结束位置 this.endX = e.changedTouches[0].clientX; // 左滑 if( parentElement.dataset.type == 0 && this.startX - this.endX > 30 ){ this.restSlide(); parentElement.dataset.type = 1; } // 右滑 if( parentElement.dataset.type == 1 && this.startX - this.endX < -30 ){ this.restSlide(); parentElement.dataset.type = 0; } this.startX = 0; this.endX = 0; }, //判断当前是否有滑块处于滑动状态 checkSlide(){ let listItems = document.querySelectorAll('.list-item'); for( let i = 0 ; i < listItems.length ; i++){ if( listItems[i].dataset.type == 1 ) { return true; } } return false; }, //复位滑动状态 restSlide(){ let listItems = document.querySelectorAll('.list-item'); // 复位 for( let i = 0 ; i < listItems.length ; i++){ listItems[i].dataset.type = 0; } }, //删除 deleteItem(e){ // 当前索引 let index = e.currentTarget.dataset.index; // 复位 this.restSlide(); // 删除 this.list.splice(index,1); } } }The js code is just this, each function There are comments and explanations, I believe everyone can understand them, so I won’t explain them too much. The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. Related articles:
Use vue to write a sample code for a carousel chart that imitates a simple book
Solution to vue- Loader issues in cli-created projects
The above is the detailed content of Vue imitates QQ left slide to delete component function. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools