微信小程式遇到的問題總結
阿新 • • 發佈:2019-01-20
1、事件
問題描述:本來想點選的時候去做某些事情,長按的時候去做另一些事情,然而發現長按事件執行之後觸發了點選tap的事件。。。
然後測試了小程式的事件流程,單擊、雙擊、長按最終都會觸發tap,呵呵呵呵。。。
解決方案:通過觸控事件來進行判斷是執行tap的事件還是longtap的事件(我要這longtap有何用???還是要自己通過觸控時間來判斷)
Page({
data: {
loanNum: null,
loanName: null,
files: [],
imgTouchStartTime: null,
imgTouchEndTime: null
},
onLoad: function (options) {
// 生命週期函式--監聽頁面載入
this.setData({
loanNum: options.loannum,
loanName: options.loanname
})
},
onReady: function () {
// 生命週期函式--監聽頁面初次渲染完成
},
onShow: function () {
// 生命週期函式--監聽頁面顯示
},
onHide: function () {
// 生命週期函式--監聽頁面隱藏
},
onUnload: function () {
// 生命週期函式--監聽頁面解除安裝
},
onPullDownRefresh: function () {
// 頁面相關事件處理函式--監聽使用者下拉動作
},
onReachBottom: function () {
// 頁面上拉觸底事件的處理函式
},
onShareAppMessage: function () {
// 使用者點選右上角分享
return {
title: 'title', // 分享標題
desc: 'desc', // 分享描述
path: 'path' // 分享路徑
}
},
chooseImage: function (e) {
var that = this;
wx.chooseImage({
count: 5, // 預設9
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有
sourceType: ['album', 'camera'], // 可以指定來源是相簿還是相機,預設二者都有
success: function (res) {
console.log(res)
// 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片
var tempFilePaths = res.tempFilePaths // 陣列型別
that.setData({
files: that.data.files.concat(tempFilePaths)
})
wx.uploadFile({
url: 'https://String',
filePath: 'tempFilePaths[0]', // 要上傳檔案資源的路徑
name: 'confirmImg',
header: { // 設定請求的 header
"Content-Type": "multipart/form-data"
},
// formData: {}, // HTTP 請求中其他額外的 form data
success: function (res) {
// success
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
fail: function () {
}
})
},
imgTap: function (e) {
console.log(e.currentTarget.dataset.src);
var that = this;
var touchTime = this.data.imgTouchEndTime - this.data.imgTouchStartTime;
console.log(touchTime)
if (touchTime < 350) { // 單機預覽
wx.previewImage({
current: e.currentTarget.dataset.src, // 當前顯示圖片的http連結
urls: this.data.files // 需要預覽的圖片http連結列表
});
} else { // 長按刪除
wx.showModal({
title: '提示',
content: '確定要刪除這張圖片嗎?',
success: function (res) {
if (res.confirm) {
console.log('使用者點選確定')
}
}
})
}
},
imgTouchStart: function (e) {
this.setData({
imgTouchStartTime: e.timeStamp
})
},
imgTouchEnd: function (e) {
this.setData({
imgTouchEndTime: e.timeStamp
})
}
})