微信小程序?qū)崿F(xiàn)圖片上傳放大預覽刪除代碼
更新時間:2020年06月28日 11:01:23 作者:nzz_171214
這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)圖片上傳放大預覽刪除代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序圖片上傳放大預覽的具體代碼,供大家參考,具體內(nèi)容如下
效果:

image.js代碼:
Page({
//選擇相冊或拍照
data: {
imgs: []
},
//上傳圖片
chooseImg: function (e) {
var that = this;
var imgs = this.data.imgs;
if (imgs.length >= 9) {
this.setData({
lenMore: 1
});
setTimeout(function () {
that.setData({
lenMore: 0
});
}, 2500);
return false;
}
wx.chooseImage({
// count: 1, // 默認9
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
success: function (res) {
// 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片
var tempFilePaths = res.tempFilePaths;
var imgs = that.data.imgs;
// console.log(tempFilePaths + '----');
for (var i = 0; i < tempFilePaths.length; i++) {
if (imgs.length >= 9) {
that.setData({
imgs: imgs
});
return false;
} else {
imgs.push(tempFilePaths[i]);
}
}
// console.log(imgs);
that.setData({
imgs: imgs
});
}
});
},
// 刪除圖片
deleteImg: function (e) {
var that = this;
var imgs = that.data.imgs;
var index = e.currentTarget.dataset.index;//獲取當前長按圖片下標
wx.showModal({
title: '提示',
content: '確定要刪除此圖片嗎?',
success: function (res) {
if (res.confirm) {
console.log('點擊確定了');
imgs.splice(index, 1);
} else if (res.cancel) {
console.log('點擊取消了');
return false;
}
that.setData({
imgs: imgs
});
}
})
},
// 預覽圖片
previewImg: function (e) {
//獲取當前圖片的下標
var index = e.currentTarget.dataset.index;
//所有圖片
var imgs = this.data.imgs;
wx.previewImage({
//當前顯示圖片
current: imgs[index],
//所有圖片
urls: imgs
})
}
})
image.wxml代碼:
<button class="upload-img-btn" bindtap="chooseImg">上傳</button>
<view class="img" wx:for="{{imgs}}" wx:for-item="item" wx:key="*this">
<image src="{{item}}" data-index="{{index}}" mode="widthFix" bindtap="previewImg" bindlongpress="deleteImg"></image>
</view>
保存到服務器上的代碼未完待續(xù)。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript TypeScript實現(xiàn)貪吃蛇游戲完整詳細流程
這篇文章主要介紹了JavaScript TypeScript實現(xiàn)貪吃蛇游戲流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09
JS實現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼
這篇文章主要介紹了JS實現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼,涉及文鼎字及css樣式的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
驗證javascript中Object和Function的關系的三段簡單代碼
今天重溫經(jīng)典書籍。這一次看的是博客園李戰(zhàn)老師寫的<<悟透JavaScript>>,也是被樓豬翻看最多的技術書籍之一。2010-06-06

