uniapp 简单实现列表左滑操作

先看效果,还挺丝滑

一、页面代码,东西不多,主要注意

开始触发 @touchstart="drawStart"

滑动 @touchmove="drawMove"

触摸滑动结束@touchend="drawEnd"

html 复制代码
<template>
    <view class="container">
       <view class="main">
       	<view v-for="(item, index) in shareList" :key="index" :data-index="index" class="order-item"
       	    @touchstart="drawStart" @touchmove="drawMove" @touchend="drawEnd" :style="'right:'+item.right+'px'">
       	    <view class="content">
       			<view class="active" ></view>
       	       <image class="userlogo" :src="item.logo"></image>
       	       <view class="zbname">
       	       	{{item.title}}
       	       </view>
       	       <view class="zbcount">
       	       	&nbsp;&nbsp;[共享]
       	       </view>
       	       <button class="yqclass" open-type="share">邀请成员</button>
       	    </view>
       		<view class="zhiding" @click="zdData(item)">置顶</view>
       	    <view class="remove" @click="delData(item)">删除</view>
       	    <view class="edit" @click="editData(item)">编辑</view>
       	</view>
       </view>

    </view>
</template>

二、js 代码

javascript 复制代码
 export default {
        data() {
            return { //列表数据,可根据自己的业务获取
                //列表数据,可根据自己的业务获取
                shareList: [{
					id:0,
					logo:'/static/logo.png',
                    title: '测试1',
                    right: 0
                },
				{
					id:1,
					logo:'/static/logo.png',
				    title: '测试2',
				    right: 0
				},
				{
					id:2,
					logo:'/static/logo.png',
				    title: '测试3',
				    right: 0
				}
				],
                //左滑默认宽度
                delBtnWidth: 90,
            }
        },
        methods: {
            //开始触摸滑动
            drawStart(e) {
                console.log("开始触发");
                var touch = e.touches[0];
                this.startX = touch.clientX;
            },
            //触摸滑动
            drawMove(e) {
                console.log("滑动");
                for (var index in this.shareList) {
                    this.$set(this.shareList[index], 'right', 0);
                }
                var touch = e.touches[0];
                var item = this.shareList[e.currentTarget.dataset.index];
                var disX = this.startX - touch.clientX;
                if (disX >= 20) {
                    if (disX > this.delBtnWidth) {
                        disX = this.delBtnWidth;
                    }
                    this.$set(this.shareList[e.currentTarget.dataset.index], 'right', disX);
                } else {
                    this.$set(this.shareList[e.currentTarget.dataset.index], 'right', 0);
                }
            },
            //触摸滑动结束
            drawEnd(e) {
                console.log("滑动结束");
                var item = this.shareList[e.currentTarget.dataset.index];
                if (item.right >= this.delBtnWidth / 2) {
                    this.$set(this.shareList[e.currentTarget.dataset.index], 'right', this.delBtnWidth*2);
                } else {
                    this.$set(this.shareList[e.currentTarget.dataset.index], 'right', 0);
                }
            }

        }
    }

三、css 也是很简单

javascript 复制代码
<style lang="scss" scoped>
 
    .main {
        width: 100%;
        height: auto;
        margin: 10px auto;
        overflow: hidden
    }

    .order-item {
            width: 96%;
            display: flex;
            position: relative;
            margin: 10px auto;
            align-items: right;
            flex-direction: row;
    		border-radius: 5px;
    		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
    
        .content {
            width: 100%;
            margin: 0 auto;
        }
    	 .remove, .edit,.zhiding{
    		 width: 50px;
    		 height: 100%;
    		 color: #fff;
    		 position: absolute;
    		 top: 0;
    		 display: flex;
    		 justify-content: center;
    		 align-items: center;
    		 font-size: 14px;
    		 border-radius: 12rpx;
    	 }
        .remove {
            right: -180px;
            margin-left: -5%;    
            background-color: #fa4659;
        }
    	.zhiding {
            right: -60px;
            background-color: #08d9d6;
        }
        .edit {
    		right: -120px;
            background-color: #17b978;
        }
	.yqclass{
	    float: right;
	    font-weight: 600;
	    margin: 20px;
	    line-height: 16px;
	    border-radius: 5px;
		color: #fff;
		color: #fff;
		background-color: rgba(128, 128, 128, 0.1);
	}
	.yqclass::after {
		 border: none;
		}
	 
	.yqclass{
		font-size: 14px;
		padding: 10px 15px;
		 box-shadow: 0 4px 20px rgba(0, 0, 0, 1);
	}
	.zbname,.zbcount{
	    float: left;
	    font-weight: 600;
	    padding: 30px 0 0 0;
	    line-height: 16px;
	}
	.zbname{
		 color: #fff;
		 font-size: 16px;
	}
	.zbcount{
		color: #fff;
		font-size: 14px;
	}
	.userlogo {
	    float: left;
	    width: 40px;
	    height: 40px;
	    border-radius: 10px;
	    margin: 20px;
	}
</style>

很简单吧!

相关推荐
2501_915921431 天前
uni-app 上架 iOS 的完整流程(无需依赖 Mac)
android·macos·ios·小程序·uni-app·iphone·webview
于先生吖2 天前
前后端分离二手商城开发,质检登记、回收回款整套业务源码部署教程
java·开发语言·uni-app
Geek_Vison2 天前
政务一网通APP如何引入AI能力,通过一个AI助手就能够调用所有的功能,实现对话即办事
人工智能·ai·小程序·uni-app·小程序容器
狗凯之家源码网3 天前
UniApp 数藏系统源码部署与定制开发全指南
uni-app
RuoyiOffice4 天前
2026 企业定制开发选型:从零开发、低代码、SaaS 与 RuoYi Office 怎么选?
spring boot·uni-app·开源·saas·oa·定制化·ruoyioffice
三天不学习4 天前
【超详细】Vue3+UniApp+.NET8集成腾讯云IM即时通信全攻略
uni-app·.net·腾讯云·im·即时通信
于先生吖4 天前
前后端分离人事招聘项目,校招宣讲预约+社招双向撮合功能架构设计教程
java·开发语言·uni-app
QQ_5110082854 天前
uniapp微信小程序网上饰品商城售卖系统php python物流
微信小程序·uni-app·php
2501_915909065 天前
深入解析Mock.js:功能、应用及实战案例,提升前端开发效率
android·ios·小程序·https·uni-app·iphone·webview
于先生吖5 天前
前后端分离体育服务项目,场馆计费+线下赛事排行小程序部署开发教程
java·小程序·uni-app