小程序開發:左滑刪除

導語

首先聲明兩點:html

  1. 思路以及代碼,是根據資料進行一些修改以及補充,原文地址在此
  2. 下面的只是 demo,各位根據本身的須要進行修改完善

實現的思路摘抄以下canvas

1,首先頁面每一個item分爲上下兩層,上面一層放置正常內容,下面一層放置左滑顯示出的按鈕,這個能夠使用z-index來實現分層。

2,item上層使用絕對定位,咱們操縱 left 屬性的值來實現像左移動。小程序

3,咱們經過微信小程序api提供的touch對象和3個有關手指觸摸的函數(touchstart,touchmove,touchend)來實現item隨手指移動。微信小程序

頁面部分

在頁面中沒有太複雜的邏輯,除了正常的循環輸出數據,須要添加綁定 touch 事件。api

<view wx:for="{{array}}">
    <view bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" style="{{item.txtStyle}}" data-index="{{index}}">
      <!-- 省略數據 -->

    </view>
    <view  catchtap="delOrder" data-index='{{index}}' data-order_id='{{item.order_id}}'>刪除</view>
</view>

JS 部分

JS 中根據綁定的 touch 事件觸發刪除按鈕,用戶點擊刪除,發送請求,根據返回值對用戶進行反饋。微信

Page({

    /**
     * 頁面的初始數據
     */
    data: {
        array:[],
        delBtnWidth: 150//刪除按鈕寬度單位(rpx)
    },

    /**
     * 手指觸摸開始
     */
    touchS: function (e) {
        //判斷是否只有一個觸摸點
        if (e.touches.length == 1) {
            this.setData({
                //記錄觸摸起始位置的X座標
                startX: e.touches[0].clientX
            });
        }
    },

    /**
     * 手指觸摸滑動
     */
    touchM: function (e) {
        var that = this;
        if (e.touches.length == 1) {
            //記錄觸摸點位置的X座標
            var moveX = e.touches[0].clientX;
            //計算手指起始點的X座標與當前觸摸點的X座標的差值
            var disX = that.data.startX - moveX;
            //delBtnWidth 爲右側按鈕區域的寬度
            var delBtnWidth = that.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";
                }
            }
            //獲取手指觸摸的是哪個item
            var index = e.currentTarget.dataset.index;
            var list = that.data.array;
            //將拼接好的樣式設置到當前item中
            list[index].txtStyle = txtStyle;
            //更新列表的狀態
            this.setData({
                array: list
            });
        }
    },

    /**
     * 手指觸摸結束
     */
    touchE: function (e) {
        var that = this;
        if (e.changedTouches.length == 1) {
            //手指移動結束後觸摸點位置的X座標
            var endX = e.changedTouches[0].clientX;
            //觸摸開始與結束,手指移動的距離
            var disX = that.data.startX - endX;
            var delBtnWidth = that.data.delBtnWidth;
            //若是距離小於刪除按鈕的1/2,不顯示刪除按鈕
            var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
            //獲取手指觸摸的是哪一項
            var index = e.currentTarget.dataset.index;
            var list = that.data.array;
            list[index].txtStyle = txtStyle;
            //更新列表的狀態
            that.setData({
                array: list
            });
        }
    },

    /**
     * 刪除訂單
     */
    delOrder: function (e) {
        var order_id = e.currentTarget.dataset.order_id;
        var index = e.currentTarget.dataset.index;
        var that = this;
        // 請求接口
        wx.request({
            url: 'xxxx',
            data: {
                order_id: order_id
            },
            success: function (res) {
                if (res.data.message == 'success') {
                    // 刪除成功
                    that.delItem(index)
                } else if (res.data.message == 'error') {
                    // 刪除失敗
                }
            },
            fail: function () {
                // 網絡請求失敗
            }
        })
    },

    /**
     * 刪除頁面item
     */
    delItem: function (index) {
        var list = this.data.array
        list.splice(index, 1);
        this.setData({
            array: list
        });
    }
})

參考資料:微信小程序左滑刪除效果的實現touch 事件網絡

相關文章
相關標籤/搜索