小程序向左滑动删除

上一个小程序的项目里面做过这个功能,当时没有记录下来,今天特意做了一个小的demo放在了github上面,下次在开发中遇到的话就可以直接拿下来代码复用了。效果很简单,类似于微信扣扣删除聊天栏的效果,想左滑动,出现删除按钮,点击即可删除。

github地址:https://github.com/wangxiaoting666/swipeleft-delete

 

wxml:

<!--index.wxml-->
<view class="container">
  <scroll-view style='height:{
  {height}}px;' scroll-y='{
  {scrollY}}' class='msg-list' bindscroll='onScroll'>
    <view wx:for="{
  {msgList}}" wx:key="id" class='msg-item' animation='{
  {item.wrapAnimation}}'>
      <view id='{
  {item.id}}' class='msg' animation='{
  {item.animation}}' bindtouchstart='ontouchstart' bindtouchmove='ontouchmove' bindtouchend='ontouchend'>
        <image class='header-img' src="{
  {item.headerImg}}"></image>
        <text class='user-name'>{
  {item.carid}}</text>
        <text class='msg-text'>{
  {item.msgText}}</text>
        <image class='site-img' src="{
  {item.siteImg}}"></image>
      </view>
      <view class='msg-menu'>
        <view id='{
  {item.id}}' class='menu-delete' bindtap='onDeleteMsgTap' bindlongtap='onDeleteMsgLongtap'>
          删除
        </view>
        <view id='{
  {item.id}}' class='menu-mark' bindtap='onMarkMsgTap' bindlongtap='onMarkMsgLongtap'>
          试驾
        </view>
      </view>
    </view>
  </scroll-view>
</view>

wxss:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在微信小程序使用 `<swiper>` 组件来实现滑动显示删除或取消的效果。具体实现方式如下: 1. 在页面的 `.wxml` 文件中添加 `<swiper>` 组件,设置 `class` 和 `duration` 属性: ```html <swiper class="swiper-item" duration="{{animationDuration}}" bindchange="swiperChange"> <view class="swiper-item-content">{{item.content}}</view> <view class="swiper-item-delete" bindtap="deleteItem">删除</view> </swiper> ``` 2. 在页面的 `.wxss` 文件中设置 `.swiper-item` 和 `.swiper-item-delete` 的样式: ```css .swiper-item { height: 60px; width: 100%; display: flex; align-items: center; } .swiper-item-content { flex: 1; padding-left: 20px; } .swiper-item-delete { width: 80px; height: 60px; background-color: red; color: #fff; display: flex; justify-content: center; align-items: center; } ``` 3. 在页面的 `.js` 文件中设置 `animationDuration`、`swiperChange` 和 `deleteItem` 函数,实现滑动删除的效果: ```javascript Page({ data: { items: [ {id: 1, content: 'item 1'}, {id: 2, content: 'item 2'}, {id: 3, content: 'item 3'}, {id: 4, content: 'item 4'}, {id: 5, content: 'item 5'} ], animationDuration: 500 }, swiperChange: function(e) { if (e.detail.source == 'touch') { var animation = wx.createAnimation({ duration: this.data.animationDuration }) animation.translateX(-80).step() var index = e.currentTarget.dataset.index this.setData({ ['items[' + index + '].animation']: animation.export() }) } }, deleteItem: function(e) { var index = e.currentTarget.dataset.index this.data.items.splice(index, 1) this.setData({ items: this.data.items }) } }) ``` 这样就可以实现滑动显示删除或取消的效果了。当用户向滑动 `<swiper>` 组件时,会触发 `swiperChange` 函数,通过 `createAnimation` 创建动画实现平移的效果,并将动画绑定到对应的数据项上。当用户点击删除按钮时,会触发 `deleteItem` 函数,从数据中删除对应的项,实现删除的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值