实例|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>



目录
相关文章
|
存储 缓存 编译器
探索 Jetpack Compose 内核:深入 SlotTable 系统
探索 Jetpack Compose 内核:深入 SlotTable 系统
967 1
|
人工智能 自然语言处理 API
浅谈ModelScope
ModelScope是阿里推出的下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,其开发目标为让模型应用更简单。ModelScope希望在汇集行业领先的预训练模型,减少开发者的重复研发成本,提供更加绿色环保、开源开放的AI开发环境和模型服务,助力绿色“数字经济”事业的建设。ModelScope平台以开源的方式提供了多类优质模型,开发者可在平台上免费体验与下载使用。
|
12月前
|
前端开发 vr&ar
南京观海微电子----MOS管的放大电路分析-运用模电的理论计算验证
本文介绍功率MOS管在放大电路中的应用,通过三电阻偏置电路分析其静态工作点与动态小信号模型。结合Multisim仿真,验证MOS管是否工作在饱和区,并计算跨导、电流及电压增益,结果与理论相符,体现模电理论在实际设计中的指导作用。(239字)
|
11月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 8.19.10 (macOS, Linux, Windows) - 分布式搜索和分析引擎
Elasticsearch Enterprise 8.19.10 (macOS, Linux, Windows) - 分布式搜索和分析引擎
395 0
|
存储 Java 编译器
Kotlin学习教程(八)
Kotlin学习教程(八)
|
缓存 安全 Windows
steam安装游戏为什么磁盘写入错误?磁盘写入错误怎么办?,多种解决方案
本文汇总了解决Steam磁盘写入错误的多种方法,包括修复库、使用Steam错误修复工具、清除下载缓存、以管理员身份运行及授权文件访问权限等,适用于Windows系统,帮助用户轻松应对常见问题。
20151 5
|
人工智能 自然语言处理 API
阿里云百炼xWaytoAGI共学课DAY1 - 必须了解的企业级AI应用开发知识点
本课程旨在介绍阿里云百炼大模型平台的核心功能和应用场景,帮助开发者和技术小白快速上手,体验AI的强大能力,并探索企业级AI应用开发的可能性。
4057 85
|
数据库 数据中心 OceanBase
【赵渝强老师】OceanBase租户的资源管理
OceanBase数据库支持多租户架构,通过资源配置(Unit Config)、资源单元(Unit)和资源池(Resource Pool)实现资源隔离与管理。用户可灵活定义资源规格,分配CPU、内存等资源,并通过资源池管理租户资源分配,提升系统资源利用率和管理效率。
554 0
|
消息中间件 Prometheus 运维
最佳实践|从Producer 到 Consumer,如何有效监控 Kafka
对于运维人而言,如何安装维护一套监控系统,或如何进行技术选型,从来不是工作重点。如何借助工具对所需的应用、组件进行监控,发现并解决问题才是重中之重。随着 Prometheus 逐渐成为云原生时代可观测标准,为了帮助更多运维人用好 Prometheus,阿里云云原生团队将定期更新 Prometheus 最佳实践系列。第一期我们讲解了《最佳实践|Spring Boot 应用如何接入 Prometheus 监控》,今天将为大家带来,消息队列产品 Kafka 的监控最佳实践。
最佳实践|从Producer 到 Consumer,如何有效监控 Kafka