微信小程序左滑顯示按鈕demo


wxml結構(刪除部分代碼):

<view class="chapter-item" wx:for="{{klgData}}" data-index="{{index}}" bind:touchstart="touchS" bind:touchmove="touchM" bind:touchend="touchE" wx:key="item.id"><!-- 主要代碼 -->
    <view class="klg-content"  style="{{item.contentStyle}}"><!-- 主要代碼 -->
        <view class="left-side">
            <image wx:if="{{item.image}}" mode="aspectFill" class="chapter-image" src="{{host+item.image}}" />
        </view>
        <view class="right-side">
            <text class="chapter-text chapter-name">{{item.title}}</text>
            <text class="chapter-text chapter-time">時長:{{item.duration}}</text>
        </view>
    </view>
    <view class="operates" style="{{item.btnStyle}}"><!-- 主要代碼 -->
        <view class="klg-btn returnBtn" catchtap="removeKlg">
            <image mode="aspectFill" data-id="{{item._id}}" data-klgid="{{item.klgid}}" class="btn-icon" src="../../images/video_icon_value.png" />
            撕書
        </view>
        <view class="klg-btn shareBtn"><image  data-id="{{item.id}}" mode="aspectFill" class="btn-icon" src="../../images/video_point_icon_share.png" />分享</view>
    </view>
</view>

 

wxss相關代碼(主要是定位,已標識主要代碼):

.chapter-item{
    overflow:hidden;
    border-bottom: 1px solid #eee;
    position: relative;
    height:80px;
    display: flex;
}
.left-side,.right-side{
    float:left;
      display: inline-block;position:relative;height:58px;
}
.right-side{flex:1;}
.left-side{width:105px;padding-right:10px;flex:0 0 105px}
.chapter-name{flex:1;display:block;color:#2E3330;}
.chapter-time{display:block;position:absolute;bottom:0;left:0;}
.chapter-image{width:104px;height:58px;}
.chapter-text{display: block;}
.klg-content{position: absolute;left:12px;top:10px;}//主要代碼 絕對定位
.operates{position: absolute;top:0;overflow:hidden;z-index: 99;right:-160px;}//主要代碼 絕對定位
.klg-btn{color:#34BC67;width:80px;line-height: 60px; float: left;text-align: center;font-size: 16px;position: relative;padding-top:24px;} 
.btn
-icon{width:28px;height:28px;text-align: center;position: absolute;top:14px;left:25px;}

js相關代碼:

//這里delBtnWidth為160,存放在data里面
//
手指剛放到屏幕觸發 touchS:function(e){ console.log("touchS"+e); //判斷是否只有一個觸摸點 if(e.touches.length==1){ this.setData({ //記錄觸摸起始位置的X坐標 startX:e.touches[0].clientX }); } }, //觸摸時觸發,手指在屏幕上每移動一次,觸發一次 touchM:function(e){ console.log("touchM:"+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 contentStyle = ""; var btnStyle=""; if(disX == 0 || disX < 0){//如果移動距離小於等於0,文本層位置不變 contentStyle = "left:12px"; btnStyle = "right:-160px"; }else if(disX > 0 ){//移動距離大於0,文本層left值等於手指移動距離 contentStyle = "left:-"+disX+"px"; btnStyle = "right:"+(-160+disX)+"px"; if(disX>=delBtnWidth){ //控制手指移動距離最大值為刪除按鈕的寬度 contentStyle = "left:-"+delBtnWidth+"px"; btnStyle = "right:0px"; } } //獲取手指觸摸的是哪一個item var index = e.currentTarget.dataset.index; var list = that.data.klgData; //將拼接好的樣式設置到當前item中 list[index].contentStyle = contentStyle; list[index].btnStyle = btnStyle; //更新列表的狀態 this.setData({ klgData:list }); } }, touchE:function(e){ console.log("touchE"+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 contentStyle = disX > delBtnWidth/2 ? "left:-"+delBtnWidth+"px":"left:12px"; var btnStyle = disX > delBtnWidth/2 ? "right:0px":"right:-160px"; //獲取手指觸摸的是哪一項 var index = e.currentTarget.dataset.index; var list = that.data.klgData; list[index].contentStyle = contentStyle; list[index].btnStyle = btnStyle; //更新列表的狀態 that.setData({ klgData:list }); } }

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM