当前位置:网站首页>Vue.js Mobile end left slide delete component
Vue.js Mobile end left slide delete component
2020-11-06 01:22:00 【:::::::】
Vue.js The mobile end slides left to delete components
Left sliding deletion is very common on mobile terminals . Let's encapsulate this simple widget together . We want to be :
- When the slider does not exceed half of the delete button, it will automatically return to the starting position .
- Slide more than half to the maximum ( Delete button width )
- Try to simplify the code
Before we start , We have to put [touchEventApi][1] Make sure. . In this widget , Yes :
1. TouchEvent.touches ( Represent one individual TouchList object , Contains all contacts currently touching the touch plane Touch object )
2. TouchEvent.changedTouches ( One TouchList object , Contains all the information from the last touch event to this event ,
Of a contact whose state has changed Touch object .)
Don't talk much , Go straight to the code :
<template>
<div class="delete">
<div class="slider">
<div class="content"
@touchstart='touchStart'
@touchmove='touchMove'
@touchend='touchEnd'
:style="deleteSlider"
>
<!-- The slot contains the content needed in the specific project -->
<slot></slot>
</div>
<div class="remove" ref='remove'>
Delete
</div>
</div>
</div>
</template>
And then there was css, What I'm using here is less
<style scoped lang="less" scoped>
.slider{
width: 100%;
height:200px;
position: relative;
user-select: none;
.content{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background:green;
z-index: 100;
// Set the transition animation
transition: 0.3s;
}
.remove{
position: absolute;
width:200px;
height:200px;
background:red;
right: 0;
top: 0;
color:#fff;
text-align: center;
font-size: 40px;
line-height: 200px;
}
}
</style>
<script type="text/ecmascript-6">
export default {
data() {
return {
startX:0, // Touch the location
endX:0, // End position
moveX: 0, // Position when sliding
disX: 0, // Moving distance
deleteSlider: '',// The effect of sliding , Use v-bind:style="deleteSlider"
}
},
methods:{
touchStart(ev){
ev= ev || event
//tounches An array of class , be equal to 1 At this time, there is only one finger touching the screen
if(ev.touches.length == 1){
// Record the starting position
this.startX = ev.touches[0].clientX;
}
},
touchMove(ev){
ev = ev || event;
// Get the width of the delete button , This width is the maximum distance the slider slides to the left
let wd=this.$refs.remove.offsetWidth;
if(ev.touches.length == 1) {
// Real time distance from left side of browser when sliding
this.moveX = ev.touches[0].clientX
// Start position minus Real time sliding distance , Get the finger offset in real time
this.disX = this.startX - this.moveX;
console.log(this.disX)
// If it's sliding right or not , Don't change the position of the slider
if(this.disX < 0 || this.disX == 0) {
this.deleteSlider = "transform:translateX(0px)";
// Greater than 0, It means left slip , Now the slider starts to slide
}else if (this.disX > 0) {
// The specific sliding distance I take is Finger offset *5.
this.deleteSlider = "transform:translateX(-" + this.disX*5 + "px)";
// The maximum can only be equal to the width of the delete button
if (this.disX*5 >=wd) {
this.deleteSlider = "transform:translateX(-" +wd+ "px)";
}
}
}
},
touchEnd(ev){
ev = ev || event;
let wd=this.$refs.remove.offsetWidth;
if (ev.changedTouches.length == 1) {
let endX = ev.changedTouches[0].clientX;
this.disX = this.startX - endX;
console.log(this.disX)
// If the distance is less than half of the delete button , Forced back to the starting point
if ((this.disX*5) < (wd/2)) {
this.deleteSlider = "transform:translateX(0px)";
}else{
// More than half Slide to maximum
this.deleteSlider = "transform:translateX(-"+wd+ "px)";
}
}
}
}
}
</script>
It's all done here , Hopefully that helped ! I hope you can point out the deficiencies !
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
边栏推荐
- The practice of the architecture of Internet public opinion system
- Skywalking series blog 5-apm-customize-enhance-plugin
- Python + appium automatic operation wechat is enough
- 阿里云Q2营收破纪录背后,云的打开方式正在重塑
- 熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
- How do the general bottom buried points do?
- Linked blocking Queue Analysis of blocking queue
- CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
- It's so embarrassing, fans broke ten thousand, used for a year!
- 如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
猜你喜欢

Summary of common algorithms of binary tree

一篇文章教会你使用HTML5 SVG 标签
![[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor](/img/cc/17b647d403c7a1c8deb581dcbbfc2f.jpg)
[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor

人工智能学什么课程?它将替代人类工作?

向北京集结!OpenI/O 2020启智开发者大会进入倒计时

The difference between Es5 class and ES6 class

一篇文章带你了解CSS3图片边框

vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证

Did you blog today?

Using Es5 to realize the class of ES6
随机推荐
助力金融科技创新发展,ATFX走在行业最前列
[event center azure event hub] interpretation of error information found in event hub logs
多机器人行情共享解决方案
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
EOS创始人BM: UE,UBI,URI有什么区别?
Elasticsearch 第六篇:聚合統計查詢
Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】
Natural language processing - wrong word recognition (based on Python) kenlm, pycorrector
git rebase的時候捅婁子了,怎麼辦?線上等……
钻石标准--Diamond Standard
Relationship between business policies, business rules, business processes and business master data - modern analysis
做外包真的很难,身为外包的我也无奈叹息。
ES6学习笔记(四):教你轻松搞懂ES6的新增语法
在大规模 Kubernetes 集群上实现高 SLO 的方法
Programmer introspection checklist
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Vuejs development specification
Skywalking series blog 2-skywalking using
你的财务报告该换个高级的套路了——财务分析驾驶舱
Linked blocking Queue Analysis of blocking queue