微信小程序 頁面滑動事件的實(shí)例詳解
更新時間:2017年10月12日 14:35:18 作者:漠漠~
這篇文章主要介紹了微信小程序 頁面滑動事件的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
微信小程序——頁面滑動事件
wxml:
<view id="id" class = "ball" bindtap = "handletap" bindtouchstart = "handletouchtart" bindtouchmove="handletouchmove" bindtouchend="handletouchend" style = "width : 100%px; height : 40px;">
{{text}}
</view>
wxss:
.ball {
box-shadow:2px 2px 10px #AAA;
border-radius: 20px;
position: absolute;
}
js:
//js
Page({
data: {
lastX: 0, //滑動開始x軸位置
lastY: 0, //滑動開始y軸位置
text: "沒有滑動",
currentGesture: 0, //標(biāo)識手勢
},
//滑動移動事件
handletouchmove: function (event) {
var currentX = event.touches[0].pageX
var currentY = event.touches[0].pageY
var tx = currentX - this.data.lastX
var ty = currentY - this.data.lastY
var text = ""
//左右方向滑動
if (Math.abs(tx) > Math.abs(ty)) {
if (tx < 0)
text = "向左滑動"
else if (tx > 0)
text = "向右滑動"
}
//上下方向滑動
else {
if (ty < 0)
text = "向上滑動"
else if (ty > 0)
text = "向下滑動"
}
//將當(dāng)前坐標(biāo)進(jìn)行保存以進(jìn)行下一次計(jì)算
this.data.lastX = currentX
this.data.lastY = currentY
this.setData({
text: text,
});
},
//滑動開始事件
handletouchtart: function (event) {
this.data.lastX = event.touches[0].pageX
this.data.lastY = event.touches[0].pageY
},
//滑動結(jié)束事件
handletouchend: function (event) {
this.data.currentGesture = 0;
this.setData({
text: "沒有滑動",
});
},
})
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
基于JavaScript ES新特性let與const關(guān)鍵字
這篇文章主要介紹了基于JavaScript ES新特性let與const關(guān)鍵字,let是ECMAScript 2015新增的一個關(guān)鍵字,用于聲明變量,const關(guān)鍵字用于聲明一個常量,更多詳細(xì)內(nèi)容,請需要的小伙伴參考下面文章的介紹,希望對你有所幫助2021-12-12
自定義range?sliders滑塊實(shí)現(xiàn)元素拖動方法
這篇文章主要為大家介紹了自定義range?sliders滑塊實(shí)現(xiàn)元素拖動方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
實(shí)現(xiàn)一個簡單得數(shù)據(jù)響應(yīng)系統(tǒng)
這篇文章主要介紹了實(shí)現(xiàn)一個簡單得數(shù)據(jù)響應(yīng)系統(tǒng),文章介紹的數(shù)據(jù)響應(yīng)系統(tǒng)會用到Dep,其實(shí),這就是一個依賴收集的容器, depend 收集依賴, notify 觸發(fā)依賴,下面來看看詳細(xì)的內(nèi)容結(jié)介紹,需要的朋友可以參考一下2021-11-11

