react 滑动删除组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import React from "react" ; import "./style.less" ; class SlideItem extends React.Component { constructor(props) { super (props); this .state = {}; } componentDidMount() {} handleTouchStart = e => { this .startX = e.touches[0].pageX; this .startY = e.touches[0].pageY; } handleTouchMove = e => { // 若想阻止冒泡且最外层盒子为scrollView,不可用e.stopPropogagation(),否则页面卡死 this .currentX = e.touches[0].pageX; this .moveX = this .currentX - this .startX; this .moveY = e.touches[0].pageY - this .startY; // 纵向移动时return if (Math.abs( this .moveY) > Math.abs( this .moveX)) { return ; } // 滑动超过一定距离时,才触发 if (Math.abs( this .moveX) < 10) { return ; } const distance = this .moveX >= 0 ? 0 : -70; this .setState({ moveStyle: { transform:`translateX(${distance}px)` } }); } render() { let { moveStyle } = this .state; return ( <React.Fragment> <div className= "slide-item-wrap" > <div className= "slide-item-children" style={moveStyle} onTouchStart={ this .handleTouchStart} onTouchMove={ this .handleTouchMove} > { this .props.children } </div> <div className= "delete-btn" onClick={() => this .props.onDelete()} > 删除 </div> </div> </React.Fragment> ); } } export default SlideItem; |
样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | .slide-item-wrap { width : 345px ; height : 86px ; background : #FFFFFF ; border-radius: 5px ; position : relative ; overflow : hidden ; margin-bottom : 12px ; .slide-item-children { position : absolute ; top : 0 ; left : 0 ; width : 100% ; height : 100% ; z-index : 2 ; transition: transform 0.3 s ease; } .delete-btn { position : absolute ; top : 0 ; right : 0 ; width : 70px ; height : 100% ; background : #FC5A45 ; z-index : 1 ; display : flex; align-items: center ; justify- content : center ; font-size : 13px ; color : #FFFFFF ; border-radius: 0 5px 5px 0 ; } } |
https://segmentfault.com/a/1190000019654209?utm_source=tag-newest
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!