知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序左滑顯示按鈕demo
發(fā)表時(shí)間:2021-3-31
發(fā)布人:葵宇科技
瀏覽次數(shù):76
wxml結(jié)構(gòu)(刪除部分代碼):
<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="https://www.wxapp-union.com/{{host+item.image}}" />
</view>
<view class="right-side">
<text class="chapter-text chapter-name">{{item.title}}</text>
<text class="chapter-text chapter-time">時(shí)長:{{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="https://www.wxapp-union.com/images/video_icon_value.png" />
撕書
</view>
<view class="klg-btn shareBtn"><image data-id="{{item.id}}" mode="aspectFill" class="btn-icon" src="https://www.wxapp-union.com/images/video_point_icon_share.png" />分享</view>
</view>
</view>
wxss相關(guān)代碼(主要是定位,已標(biāo)識(shí)主要代碼):
.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;}//主要代碼 絕對(duì)定位
.operates{position: absolute;top:0;overflow:hidden;z-index: 99;right:-160px;}//主要代碼 絕對(duì)定位
.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相關(guān)代碼:
//這里delBtnWidth為160,存放在data里面
//手指剛放到屏幕觸發(fā)
touchS:function(e){
console.log("touchS"+e);
//判斷是否只有一個(gè)觸摸點(diǎn)
if(e.touches.length==1){
this.setData({
//記錄觸摸起始位置的X坐標(biāo)
startX:e.touches[0].clientX
});
}
},
//觸摸時(shí)觸發(fā),手指在屏幕上每移動(dòng)一次,觸發(fā)一次
touchM:function(e){
console.log("touchM:"+e);
var that = this
if(e.touches.length==1){
//記錄觸摸點(diǎn)位置的X坐標(biāo)
var moveX = e.touches[0].clientX;
//計(jì)算手指起始點(diǎn)的X坐標(biāo)與當(dāng)前觸摸點(diǎn)的X坐標(biāo)的差值
var disX = that.data.startX - moveX;
//delBtnWidth 為右側(cè)按鈕區(qū)域的寬度
var delBtnWidth = that.data.delBtnWidth;
var contentStyle = "";
var btnStyle="";
if(disX == 0 || disX < 0){//如果移動(dòng)距離小于等于0,文本層位置不變
contentStyle = "left:12px";
btnStyle = "right:-160px";
}else if(disX > 0 ){//移動(dòng)距離大于0,文本層left值等于手指移動(dòng)距離
contentStyle = "left:-"+disX+"px";
btnStyle = "right:"+(-160+disX)+"px";
if(disX>=delBtnWidth){
//控制手指移動(dòng)距離最大值為刪除按鈕的寬度
contentStyle = "left:-"+delBtnWidth+"px";
btnStyle = "right:0px";
}
}
//獲取手指觸摸的是哪一個(gè)item
var index = e.currentTarget.dataset.index;
var list = that.data.klgData;
//將拼接好的樣式設(shè)置到當(dāng)前item中
list[index].contentStyle = contentStyle;
list[index].btnStyle = btnStyle;
//更新列表的狀態(tài)
this.setData({
klgData:list
});
}
},
touchE:function(e){
console.log("touchE"+e);
var that = this
if(e.changedTouches.length==1){
//手指移動(dòng)結(jié)束后觸摸點(diǎn)位置的X坐標(biāo)
var endX = e.changedTouches[0].clientX;
//觸摸開始與結(jié)束,手指移動(dòng)的距離
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";
//獲取手指觸摸的是哪一項(xiàng)
var index = e.currentTarget.dataset.index;
var list = that.data.klgData;
list[index].contentStyle = contentStyle;
list[index].btnStyle = btnStyle;
//更新列表的狀態(tài)
that.setData({
klgData:list
});
}
}
結(jié)果:
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)絡(luò)營銷顧問
- 云南網(wǎng)站建設(shè)專家
- 網(wǎng)絡(luò)營銷
- 云南網(wǎng)站建設(shè)哪家好
- 人人商城
- flex
- 生成海報(bào)
- 汽車報(bào)廢回收管理軟件
- 智慧農(nóng)貿(mào)市場
- 網(wǎng)站建設(shè)選
- 網(wǎng)站排名
- 云南網(wǎng)站建設(shè)快速優(yōu)化
- 小程序開發(fā)聯(lián)系方式
- 小程序定制開發(fā)
- web
- 云南省住房建設(shè)廳網(wǎng)站
- 云南網(wǎng)站建設(shè)服務(wù)
- 前端
- 網(wǎng)站建設(shè)
- 昆明軟件公司
- 報(bào)廢車管理
- 做小程序被騙
- 支付寶小程序被騙
- 汽車回收管理系統(tǒng)
- 迪慶小程序開發(fā)
- 汽車回收系統(tǒng)
- 文山小程序開發(fā)
- 小程序商城
- 貴州小程序開發(fā)
- Web開發(fā)框架