本文實(shí)例為大家分享了微信小程序圖片上傳放大預(yù)覽的具體代碼,供大家參考,具體內(nèi)容如下
效果:
image.js代碼:
Page({ //選擇相冊(cè)或拍照 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, // 默認(rèn)9 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來(lái)源是相冊(cè)還是相機(jī),默認(rèn)二者都有 success: function (res) { // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的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;//獲取當(dāng)前長(zhǎng)按圖片下標(biāo) wx.showModal({ title: '提示', content: '確定要?jiǎng)h除此圖片嗎?', success: function (res) { if (res.confirm) { console.log('點(diǎn)擊確定了'); imgs.splice(index, 1); } else if (res.cancel) { console.log('點(diǎn)擊取消了'); return false; } that.setData({ imgs: imgs }); } }) }, // 預(yù)覽圖片 previewImg: function (e) { //獲取當(dāng)前圖片的下標(biāo) var index = e.currentTarget.dataset.index; //所有圖片 var imgs = this.data.imgs; wx.previewImage({ //當(dāng)前顯示圖片 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>
保存到服務(wù)器上的代碼未完待續(xù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注