微信小程序實現運動步數排行(可刪除)


wxmljavascript

<!-- 向左滑動刪除功能 -->
<view class="item-box">
  <view class="items">
    <view wx:for="{{list}}" wx:key="{{index}}" class="item">
     
      <view bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index="{{index}}" style="{{item.txtStyle}}" class="inner txt">
      <i>{{item.rank}}</i>
      <image class="item-icon" mode="widthFix" src="{{item.icon}}"></image>
       <i> {{item.name}}</i>
      <span class="item-data">
    
       <i class="rankpace"> {{item.pace}}</i>
        <!-- <span class="rankxin">{{item.xin}}</span> -->
      </span>
      
      </view>
     
      <view data-index="{{index}}" bindtap = "delItem" class="inner del">刪除</view>
    </view>
  </view>
</view>

wxsscss

/* pages/leftSwiperDel/index.wxss */ view{ box-sizing: border-box; } .item-box{ width: 700rpx; margin: 0 auto; padding:40rpx 0; } .items{ width: 100%; } .item{ position: relative; border-top: 2rpx solid #eee; height: 120rpx; line-height: 120rpx; overflow: hidden; } .item:last-child{ border-bottom: 2rpx solid #eee; } .inner{ position: absolute; top:0; } .inner.txt{ background-color: #fff; width: 100%; z-index: 5; padding:0 10rpx; transition: left 0.2s ease-in-out; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; } .inner.del{ background-color: #e64340; width: 180rpx;text-align: center; z-index: 4; right: 0; color: #fff } .item-icon{ width: 64rpx; height: 64rpx; vertical-align: middle; margin-right: 16rpx; margin-left:13px; border-radius:50%; } .item-data{ float: right; margin-right:5%;} .rankpace{ color: #fa7e04; } /* pages/leftSwiperDel/index.wxss */ view{ box-sizing: border-box; } .item-box{ width: 700rpx; margin: 0 auto; padding:40rpx 0; } .items{ width: 100%; } .item{ position: relative; border-top: 2rpx solid #eee; height: 120rpx; line-height: 120rpx; overflow: hidden; } .item:last-child{ border-bottom: 2rpx solid #eee; } .inner{ position: absolute; top:0; } .inner.txt{ background-color: #fff; width: 100%; z-index: 5; padding:0 10rpx; transition: left 0.2s ease-in-out; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; } .inner.del{ background-color: #e64340; width: 180rpx;text-align: center; z-index: 4; right: 0; color: #fff } .item-icon{ width: 64rpx; height: 64rpx; vertical-align: middle; margin-right: 16rpx; margin-left:13px; border-radius:50%; } .item-data{ float: right; margin-right:5%;} .rankpace{ color: #fa7e04; }

js前端

// pages/leftSwiperDel/index.js Page({ data: { delBtnWidth: 180//刪除按鈕寬度單位(rpx) }, onLoad: function (options) { // 頁面初始化 options爲頁面跳轉所帶來的參數 this.initEleWidth(); this.tempData(); }, onReady: function () { // 頁面渲染完成 }, onShow: function () { // 頁面顯示 }, onHide: function () { // 頁面隱藏 }, onUnload: function () { // 頁面關閉 }, touchS: function (e) { if (e.touches.length == 1) { this.setData({ //設置觸摸起始點水平方向位置 startX: e.touches[0].clientX }); } }, touchM: function (e) { if (e.touches.length == 1) { //手指移動時水平方向位置 var moveX = e.touches[0].clientX; //手指起始點位置與移動期間的差值 var disX = this.data.startX - moveX; var delBtnWidth = this.data.delBtnWidth; var txtStyle = ""; if (disX == 0 || disX < 0) {//若是移動距離小於等於0,文本層位置不變 txtStyle = "left:0px"; } else if (disX > 0) {//移動距離大於0,文本層left值等於手指移動距離 txtStyle = "left:-" + disX + "px"; if (disX >= delBtnWidth) { //控制手指移動距離最大值爲刪除按鈕的寬度 txtStyle = "left:-" + delBtnWidth + "px"; } } //獲取手指觸摸的是哪一項 var index = e.target.dataset.index; var list = this.data.list; list[index].txtStyle = txtStyle; //更新列表的狀態 this.setData({ list: list }); } }, touchE: function (e) { if (e.changedTouches.length == 1) { //手指移動結束後水平位置 var endX = e.changedTouches[0].clientX; //觸摸開始與結束,手指移動的距離 var disX = this.data.startX - endX; var delBtnWidth = this.data.delBtnWidth; //若是距離小於刪除按鈕的1/2,不顯示刪除按鈕 var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px"; //獲取手指觸摸的是哪一項 var index = e.target.dataset.index; var list = this.data.list; list[index].txtStyle = txtStyle; //更新列表的狀態 this.setData({ list: list }); } }, //獲取元素自適應後的實際寬度 getEleWidth: function (w) { var real = 0; try { var res = wx.getSystemInfoSync().windowWidth; var scale = (750 / 2) / (w / 2);//以寬度750px設計稿作寬度的自適應 // console.log(scale); real = Math.floor(res / scale); return real; } catch (e) { return false; // Do something when catch error } }, initEleWidth: function () { var delBtnWidth = this.getEleWidth(this.data.delBtnWidth); this.setData({ delBtnWidth: delBtnWidth }); }, //點擊刪除按鈕事件 delItem: function (e) { //獲取列表中要刪除項的下標 var index = e.target.dataset.index; var list = this.data.list; //移除列表中下標爲index的項 list.splice(index, 1); //更新列表的狀態 this.setData({ list: list }); }, //測試臨時數據 tempData: function () { var list = [ { rank: "1", txtStyle: "", icon: "/images/my.png", name: "李飛", pace: "23456", }, { rank: "2", txtStyle: "", icon: "/images/my.png", name: "張葉", pace: "23450", }, { rank: "3", txtStyle: "", icon: "/images/my.png", name: "王小婷", pace: "22345", }, { rank: "4", txtStyle: "", icon: "/images/my.png", name: "袁經理", pace: "21687", }, { rank: "5", txtStyle: "", icon: "/images/my.png", name: "陳雅婷", pace: "21680", }, { rank: "6", txtStyle: "", icon: "/images/my.png", name: "許安琪", pace: "20890", }, { rank: "7", txtStyle: "", icon: "/images/my.png", name: "裏俊飛", pace: "20741", }, { rank: "8", txtStyle: "", icon: "/images/my.png", name: "李小俊", pace: "19511", }, { rank: "9", txtStyle: "", icon: "/images/my.png", name: "陳俊飛", pace: "19501", },] // this.setData({ list: list }); } }) // pages/leftSwiperDel/index.js Page({ data: { delBtnWidth: 180//刪除按鈕寬度單位(rpx) }, onLoad: function (options) { // 頁面初始化 options爲頁面跳轉所帶來的參數 this.initEleWidth(); this.tempData(); }, onReady: function () { // 頁面渲染完成 }, onShow: function () { // 頁面顯示 }, onHide: function () { // 頁面隱藏 }, onUnload: function () { // 頁面關閉 }, touchS: function (e) { if (e.touches.length == 1) { this.setData({ //設置觸摸起始點水平方向位置 startX: e.touches[0].clientX }); } }, touchM: function (e) { if (e.touches.length == 1) { //手指移動時水平方向位置 var moveX = e.touches[0].clientX; //手指起始點位置與移動期間的差值 var disX = this.data.startX - moveX; var delBtnWidth = this.data.delBtnWidth; var txtStyle = ""; if (disX == 0 || disX < 0) {//若是移動距離小於等於0,文本層位置不變 txtStyle = "left:0px"; } else if (disX > 0) {//移動距離大於0,文本層left值等於手指移動距離 txtStyle = "left:-" + disX + "px"; if (disX >= delBtnWidth) { //控制手指移動距離最大值爲刪除按鈕的寬度 txtStyle = "left:-" + delBtnWidth + "px"; } } //獲取手指觸摸的是哪一項 var index = e.target.dataset.index; var list = this.data.list; list[index].txtStyle = txtStyle; //更新列表的狀態 this.setData({ list: list }); } }, touchE: function (e) { if (e.changedTouches.length == 1) { //手指移動結束後水平位置 var endX = e.changedTouches[0].clientX; //觸摸開始與結束,手指移動的距離 var disX = this.data.startX - endX; var delBtnWidth = this.data.delBtnWidth; //若是距離小於刪除按鈕的1/2,不顯示刪除按鈕 var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px"; //獲取手指觸摸的是哪一項 var index = e.target.dataset.index; var list = this.data.list; list[index].txtStyle = txtStyle; //更新列表的狀態 this.setData({ list: list }); } }, //獲取元素自適應後的實際寬度 getEleWidth: function (w) { var real = 0; try { var res = wx.getSystemInfoSync().windowWidth; var scale = (750 / 2) / (w / 2);//以寬度750px設計稿作寬度的自適應 // console.log(scale); real = Math.floor(res / scale); return real; } catch (e) { return false; // Do something when catch error } }, initEleWidth: function () { var delBtnWidth = this.getEleWidth(this.data.delBtnWidth); this.setData({ delBtnWidth: delBtnWidth }); }, //點擊刪除按鈕事件 delItem: function (e) { //獲取列表中要刪除項的下標 var index = e.target.dataset.index; var list = this.data.list; //移除列表中下標爲index的項 list.splice(index, 1); //更新列表的狀態 this.setData({ list: list }); }, //測試臨時數據 tempData: function () { var list = [ { rank: "1", txtStyle: "", icon: "/images/my.png", name: "李飛", pace: "23456", }, { rank: "2", txtStyle: "", icon: "/images/my.png", name: "張葉", pace: "23450", }, { rank: "3", txtStyle: "", icon: "/images/my.png", name: "王小婷", pace: "22345", }, { rank: "4", txtStyle: "", icon: "/images/my.png", name: "袁經理", pace: "21687", }, { rank: "5", txtStyle: "", icon: "/images/my.png", name: "陳雅婷", pace: "21680", }, { rank: "6", txtStyle: "", icon: "/images/my.png", name: "許安琪", pace: "20890", }, { rank: "7", txtStyle: "", icon: "/images/my.png", name: "裏俊飛", pace: "20741", }, { rank: "8", txtStyle: "", icon: "/images/my.png", name: "李小俊", pace: "19511", }, { rank: "9", txtStyle: "", icon: "/images/my.png", name: "陳俊飛", pace: "19501", },] // this.setData({ list: list }); } })



注:
java

原文做者:祈澈姑娘技術博客:https://www.jianshu.com/u/05f416aefbe1
90後前端妹子,愛編程,愛運營,愛折騰。
堅持總結工做中遇到的技術問題,堅持記錄工做中所所思所見,歡迎你們一塊兒探討交流。程序員

關注「編程微刊」公衆號 ,在微信後臺回覆「領取資源」,獲取IT資源300G乾貨大全。

公衆號回覆「1」,拉你進程序員技術討論羣.編程

相關文章
相關標籤/搜索