实例|APICloud AVM框架封装滑动单元格组件

简介: 滑动单元格组件原理是主题部分把按钮进行遮挡,按钮通过绝对定位,定位在最右边,通过监听触摸事件(touch),判断滑动的方向和计算滑动的距离以此来判定显示和隐藏按钮。

滑动单元格组件原理是主题部分把按钮进行遮挡,按钮通过绝对定位,定位在最右边,通过监听触摸事件(touch),判断滑动的方向和计算滑动的距离以此来判定显示和隐藏按钮。显示和隐藏按钮是通过对主体部分进行css 的transform属性对主体元素进行移动,以达到显示和隐藏按钮的效果。今天介绍APICloud AVM框架封装滑动单元格组件的实例。


组件文件

easy-swipe-cell.stml


<template>

<view class="easy-swipe-cell_container" data-id={itemId} @touchstart="start" @touchmove="move" @touchend="end">

<view class="easy-swipe-cell_content" :style="itemId == touchIdNow?handleSwipe:'transform:translateX(0px)'">

<text>{itemContent}</text>

</view>

<view class="easy-swipe-cell_btn" id="btn">

<view class="easy-swipe-cell_btn-item" style="background-color: #ee0a24;" data-id={itemId} data-type='delete' @click="clickItem">

<text class="easy-swipe-cell_btn-item-label">删除</text>

</view>

<view class="easy-swipe-cell_btn-item" style="background-color: #07c160;" data-id={itemId} data-type='read' @click="clickItem">

<text class="easy-swipe-cell_btn-item-label">已读</text>

</view>

</view>

</view>

</template>

<script>

export default {

name: 'easy-swipe-cell',

props:{

itemId:String,

itemContent:String,

touchIdNow:String

},

data() {

return{

startX:0, //触摸位置

endX:0, //结束位置

moveX: 0, //滑动时的位置

disX: 0, //移动距离

handleSwipe: '',//滑动时的效果,动态绑定

touchId:''

}

},

mounted (){

 

},

methods: {

start(e){

// console.log(JSON.stringify(e.detail)+'开始');

this.data.startX = e.detail.x;

this.data.touchId = e.currentTarget.dataset.id;

this.fire('touch',this.data.touchId);

},

move(e){

// console.log(JSON.stringify(e.detail)+'移动');

let wd=document.getElementById('btn').offsetWidth;

this.data.moveX = e.detail.x;

this.data.disX = this.data.startX - this.data.moveX;

console.log(this.data.disX);

// 如果是向右滑动或者不滑动,不改变滑块的位置

if(this.disX < 0 || this.disX == 0) {

this.data.handleSwipe = "transform:translateX(0px)";

// 大于0,表示左滑了,此时滑块开始滑动

}else if (this.disX > 0) {

//具体滑动距离我取的是 手指偏移距离*5。

this.data.handleSwipe = "transform:translateX(-" + this.disX*5 + "px)";

// 最大也只能等于按钮宽度

if (this.disX*5 >=wd) {

this.handleSwipe = "transform:translateX(-" +wd+ "px)";

}

}

this.fire('touch',this.data.touchId);

},

end(e){

//console.log(JSON.stringify(e.detail)+'结束');

let wd=document.getElementById('btn').offsetWidth;

let endX = e.detail.x;

this.disX = this.data.startX - endX;

//如果距离小于按钮一半,强行回到起点

if ((this.disX*5) < (wd/2)) {

this.data.handleSwipe = "transform:translateX(0px)";

}else{

//大于一半 滑动到最大值

this.data.handleSwipe = "transform:translateX(-"+wd+ "px)";

}

this.fire('touch',this.data.touchId);

},

clickItem(e){

this.data.handleSwipe = "transform:translateX(0px)";

this.fire('clickBtn',{type:e.currentTarget.dataset.type,id:e.currentTarget.dataset.id});

}

       }

}

</script>

<style>

.easy-swipe-cell_content{

justify-content: center;

background-color: #ffffff;

position: relative;

width: 100%;

left: 0;

right: 0;

top: 0;

bottom: 0;

z-index: 1000;

transition: 0.6s;

min-height: 50px;

padding: 10px;

}

.easy-swipe-cell_btn{

position: absolute;

right: 0;

top: 0;

display: flex;

flex-flow: row nowrap;

height: 100%;

z-index: 1;

}

.easy-swipe-cell_btn-item{

height: 100%;

justify-content: center;

}

.easy-swipe-cell_btn-item-label{

color: #ffffff;

font-size: 15px;

padding: 0 20px;

}

</style>

示例文件

demo-easy-swipe-cell.stml


<template>

<scroll-view class="page">

<safe-area></safe-area>

<view v-for="(item,index) in list">

<easy-swipe-cell

:itemId="item.id"

:itemContent="item.content"

:touchIdNow="touchID"

ontouch="getTouchID"

onclickBtn="getClickTyeAndId"

>

</easy-swipe-cell>

</view>

</scroll-view>

</template>

<script>

import '../../components/easy-swipe-cell.stml'

export default {

name: 'demo-easy-swipe-cell',

apiready(){//like created

 

},

data() {

return{

list:[{

id:'1',

content:'关于开展什么活动的通知'

},{

id:'2',

content:'这是一条新的系统通知'

},{

id:'3',

content:'您有一条新的消息提醒,请及时查看'

}],

touchID:''

}

},

methods: {

getTouchID(e){

console.log(JSON.stringify(e));

this.data.touchID = e.detail;

},

getClickTyeAndId(e){

console.log(JSON.stringify(e));

api.toast({

msg:'当前点击的是'+e.detail.type+'按钮,记录ID是'+e.detail.id

})

}

}

}

</script>

<style>

.page {

height: 100%;

}

</style>



目录
相关文章
实例|APICloud AVM框架打造数字滚动组件
数字滚动组件,用于数字的动态效果展示。今天用APICloud AVM框架打造数字滚动组件。
166 0
|
JSON JavaScript 区块链
实例|APICloud AVM框架封装省市区级联选择弹框
今天介绍用APICloud AVM框架封装省市区级联选择弹框。
189 0
|
JavaScript 前端开发 小程序
APICloud AVM框架 纵向滚动通知栏组件
用于循环播放展示一组消息通知,实现了纵向滚动。
APICloud AVM框架封装数据表格组件
组件的核心功能点是在数据展示的时候,用到了2个v-for循环,第一层循环是数据对象的循环,然后嵌套列名的对象,通过列名中的key值在数据对象中查询对应的数据,这样就保证了在数据对象与列名对象顺序打乱的情况下也可以把数据对应起来,并能够在列名没有对应的数据的时候进行特殊处理。
133 0
|
开发框架 小程序 开发工具
APICloud AVM框架列表组件list-view的使用、flex布局教程
avm.js 是APICloud 推出的多端开发框架。使用 avm.js 一个技术栈可同时开发 Android & iOS 原生 App、小程序和 iOS 轻 App,且多端渲染效果统一;全新的 App 引擎 3.0 不依赖 webView,提供百分百的原生渲染,保障 App 性能和体验与原生 App 一致。
222 0
APICloud AVM框架列表组件list-view的使用、flex布局教程
APICloud AVM框架封装验证码输入框组件
APICloud AVM框架封装验证码输入框组件实例。 验证码输入框组件,可自定义下次点击等待时长,自定义验证码长度,可根据自定义的验证码长度进行输入内容的验证。
126 0
|
前端开发 JavaScript HTML5
第八章 让Bootstrap轮播插件carousel支持左右滑动手势的三种方法
因为最近开发的项目涉及到移动设备上的 HTML5 开发,其中需要实现轮播效果。然后最快捷的方式,你知道的(Bootstrap),然后原生的 Bootstrap 的 carousel.js 插件并没有支持手势。
1437 0
|
开发框架 小程序 Android开发
列表组件list-view的使用、flex布局教程,以APICloud AVM框架为例
avm.js是 APICloud 推出的多端开发框架。使用 avm.js 一个技术栈可同时开发 Android & iOS 原生 App、小程序和 iOS 轻 App,且多端渲染效果统一;全新的 App 引擎 3.0 不依赖 webView,提供百分百的原生渲染,保障 App 性能和体验与原生 App 一致。
526 0
|
JavaScript 前端开发 存储
使用Raphael绘制流程图,自绘动态箭头,可拖动,有双击事件,纯前端,兼容各种浏览器
关于Raphaël Raphaël是一个在网页上绘图的js类库,非常小压缩版只有89k左右 官方宣称兼容各种主流浏览器,据笔者测试在IE6下尚有一些问题(不过这些与本文无关) 他是使用js来创建vml或svg来绘图的 缘起 项目中不能使用Silverlight或者flash来解决绘图和拖动...
1570 0
|
算法 C# iOS开发
通通玩blend美工(6)下——仿iPhone滚动选择器的ListBox(交互逻辑)
原文:通通玩blend美工(6)下——仿iPhone滚动选择器的ListBox(交互逻辑)       上一篇我们已经把界面画出来了,这篇我们就来制作交互的逻辑吧。上一篇的电梯: http://www.
1015 0

热门文章

最新文章