微信小程序实现左滑删除效果源码

 更新时间:2023年08月17日 09:38:56   作者:人民的石头  
这篇文章主要给大家介绍了关于微信小程序实现左滑删除效果的相关资料,左滑删除效果在app的交互方式中十分流行,比如全民应用微信,文中给出了详细的实现代码,需要的朋友可以参考下

一、效果

二、代码

实现思路使用的是官方提供的movable-area 嵌套movable-view

1、movable-area:注意点,需要设置其高度,否则会出现列表内容重叠的现象。

2、由于movable-view需要向右移动,左滑的时候给删除控件展示的空间,故 movable-area 需要左移 left: -120rpx;

3、movable-view右移left: 120rpx;。 通过 width: calc(100% - 120rpx);左划的距离。

4、 需要给movable-view组件设置层级 z-index: 1001;越高越好至少比删除组件层级高,避免被遮住。

 5、 <view class="itemDelet">删除</view> 的位于movable-view组件右下,左滑movable-view显示处删除组件。

.moveArea {
  display: flex;
  flex-direction: row;
  width: calc(100% + 120rpx);
  justify-content: center;
  left: -120rpx;
  height: 188rpx;
}
movable-view
.movableView {
  display: flex;
  flex-direction: row;
  width: calc(100% - 120rpx);
  z-index: 1001;
  left: 120rpx;
}

源代码如下:

 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pushedData:[{messageTitle:'饼干',messageContent:'饼干爱吃'}],//已推送数据
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  },
  // 退出页面
  logout:function(){
    wx.navigateBack({})
  },
})

xml代码

<!--pakage_kindness_remind/pages/kindess_msg_remind/kindness_msg_remind_pg.wxml-->
<!-- head -->
<view class="title_search">
  <view class="seeck_md">
    <!-- 返回 -->
    <view class="logout" bindtap="logout">
      <image class="logout_ic" src="/images/msg/return_back.png">
      </image>
      <text class="logout_txt">返回</text>
    </view>
    <!--内容模板-->
    <view class="msg_title_center">
      <view class="msg" bindtap="open_msg">
        <text class="msg_txt">数据中心</text>
      </view>
    </view>
  </view>
  <view class="logout">
    <image class="logout_ic">
    </image>
    <text class="logout_txt"></text>
  </view>
</view>
<!-- body -->
<scroll-view class='scbg' scroll-y='true'>
  <block wx:for="{{pushedData}}" wx:key="id" wx:for-item="itemName" wx:for-index="id">
    <!-- 子item父布局 -->
    <view class="item_parent">
      <!-- 日期 4月5日 周二 8:00 -->
      <view class="date" wx:if="{{id==0}}">{{itemName.pushTime}} </view>
      <!--  -->
      <movable-area class="moveArea">
        <movable-view class="movableView" direction="horizontal" inertia="{{true}}" out-of-bounds="{{true}}">
          <view style="display: flex;flex-direction: row;width: 100%;height: 100%;">
            <view class="box_item">
              <!--head布局-->
              <view class="head_layout">
                <!-- itemhead左边模块  天气提醒-->
                <view class="head_title">
                  {{itemName.messageTitle}}
                </view>
              </view>
              <!--body布局-->
              <view class="body_layout">
                {{itemName.messageContent}}
              </view>
            </view>
          </view>
        </movable-view> 
        <view class="itemDelet">删除</view>
      </movable-area>
    </view>
  </block>
</scroll-view>

css代码

/* pakage_kindness_remind/pages/kindess_msg_remind/kindness_msg_remind_pg.wxss */
Page {
  background: #f0f0f0;
  height: 100%;
  position: fixed;
}
/* 头部搜索 */
/* 搜索标题 */
.title_search {
  background: linear-gradient(to right, #0455a7, #62c8ec);
  height: 170rpx;
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: flex-start;
}
.seeck_md {
  display: flex;
  flex-direction: row;
  width: 100%;
  justify-content: flex-start;
  align-items: flex-end;
}
/* 消息 */
.msg {
  width: 180rpx;
  height: 90rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-right: 0rpx;
  margin-left: 30rpx;
}
.msg_title_center {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: center;
}
.msg_txt {
  font-size: 36rpx;
  height: 80rpx;
  width: 160rpx;
  margin-bottom: 20rpx;
  align-items: center;
  color: #fff;
  display: flex;
  justify-content: center;
}
/* 返回 */
.logout {
  width: 100rpx;
  height: 90rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-right: 20rpx;
  margin-left: 30rpx;
}
.logout_ic {
  height: 44rpx;
  width: 48rpx;
  margin-right: 2rpx;
}
.logout_txt {
  font-size: 24rpx;
  height: 40rpx;
  width: 60rpx;
  margin-bottom: 10rpx;
  align-items: flex-start;
  color: #fff;
  display: flex;
  justify-content: flex-start;
}
/* 搜索标题 */
/* 头部搜索  */
/* body */
.scbg {
  background-color: #f0f0f0;
  width: 100%;
  height: calc(100vh - 180rpx);
  left: 0rpx;
  right: 0rpx;
  top: 0rpx;
  padding-bottom: 120rpx;
}
/* item条目布局 */
/* item父布局 */
.item_parent {
  margin-top: 20rpx;
}
.date {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999999;
  font-size: 28rpx;
  margin-bottom: 10rpx;
}
/* item盒子 */
.box_item {
  background-color: #fff;
  margin-left: 25rpx;
  margin-right: 25rpx;
  border-radius: 20rpx;
  flex-direction: column;
  width: 100%;
  height: 160rpx;
  display: flex;
}
/* item模块时间 */
/* item上部分布局 */
.head_layout {
  height: 60rpx;
  border-radius: 20rpx;
  margin-right: 20rpx;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
/* 标题 */
.head_title {
  width: 160rpx;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-left: 20rpx;
  color: #06c8ad;
  font-size: 28rpx;
  font-weight: 800;
}
/* item下部份分布局 */
.body_layout {
  background-color: #fff;
  padding-bottom: 20rpx;
  border-radius: 20rpx;
  margin-bottom: 16rpx;
  align-items: center;
  margin-left: 20rpx;
  margin-top: 10rpx;
  margin-right: 10rpx;
  font-size: 28rpx;
  color: #999999;
}
/* 滑动删除移动模块 */
.moveArea {
  display: flex;
  flex-direction: row;
  width: calc(100% + 120rpx);
  justify-content: center;
  left: -120rpx;
  height: 188rpx;
}
/* 滑动删除模块 */
.movableView {
  display: flex;
  flex-direction: row;
  width: calc(100% - 120rpx);
  z-index: 1001;
  left: 120rpx;
}
/* item删除 */
.itemDelet {
  position: absolute;
  right: 30rpx;
  line-height: 160rpx;
  background-color: #62c8ec;
  margin-top: 0rpx;
  margin-right: 6rpx;
  border-bottom-right-radius: 20rpx;
  border-top-right-radius: 20rpx;
  width: 120rpx;
  text-align: right;
  padding-right: 20rpx;
  color: #fff;
}

总结

到此这篇关于微信小程序实现左滑删除效果的文章就介绍到这了,更多相关微信小程序左滑删除内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JS函数式编程之纯函数、柯里化以及组合函数

    JS函数式编程之纯函数、柯里化以及组合函数

    这篇文章主要介绍了JS函数式编程之纯函数、柯里化以及组合函数,文章对三个函数进行分析讲解,内容也很容易理解,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-01-01
  • 微信小程序实现单选按钮

    微信小程序实现单选按钮

    这篇文章主要为大家详细介绍了微信小程序实现单选按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • JS对象数组中如何匹配某个属性值

    JS对象数组中如何匹配某个属性值

    这篇文章主要介绍了JS对象数组中如何匹配某个属性值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • javascript 变态的节点集合

    javascript 变态的节点集合

    今天想实现jQuery的unwrap效果,换言之,就是用其孩子把其父节点干掉。为了效率,用到文档碎片,而取孩子时使用到childNodes(返回一个nodeList)
    2010-03-03
  • JavaScript如何在不重新加载页面的情况下修改URL

    JavaScript如何在不重新加载页面的情况下修改URL

    在现代Web应用中,单页面应用(SPA)越来越流行,为了提升用户体验,我们经常需要在不重新加载页面的情况下修改URL,本文将详细介绍如何在不重新加载页面的情况下修改URL,并通过多个示例展示其应用场景,需要的朋友可以参考下
    2024-11-11
  • javascript中使用css需要注意的地方小结

    javascript中使用css需要注意的地方小结

    javascript中使用css需要注意的地方小结,注意保留字问题。
    2010-09-09
  • JavaScript使用键盘输入控制实现数字验证功能

    JavaScript使用键盘输入控制实现数字验证功能

    根据键盘输入的keycode来判断输入的是什么类型来实现数字验证功能,就简单几行代码就可以实现,对js数字验证功能感兴趣的朋友一起看下吧
    2016-08-08
  • JS比较两个数值的大小实例

    JS比较两个数值的大小实例

    下面小编就为大家带来一篇JS比较两个数值的大小实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • 绘制微信小程序验证码功能的实例代码

    绘制微信小程序验证码功能的实例代码

    这篇文章主要介绍了绘制微信小程序验证码功能的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • JavaScrip数组删除特定元素的几种方法总结

    JavaScrip数组删除特定元素的几种方法总结

    从js数组中删除指定元素是我们每个人都遇到的问题,网上这方面的资料也很多,但有的时间过于久远,有的内容不够全面,所以自己来整理下,这篇文章主要给大家总结介绍了关于JavaScrip数组删除特定元素的多种方法,需要的朋友可以参考下。
    2017-09-09

最新评论