微信小程序?qū)崿F(xiàn)圖片選擇并預(yù)覽功能
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)圖片選擇并預(yù)覽的具體代碼,供大家參考,具體內(nèi)容如下
(一)、功能說(shuō)明
做的是一個(gè)意見(jiàn)反饋,用戶發(fā)表意見(jiàn)和上傳圖片,限制了最多只能上傳三張圖片。
其他要點(diǎn):textarea使用,底部保存按鈕固定
(二)、小程序接口說(shuō)明
wx.chooseLocation(Object object)
從本地相冊(cè)選擇圖片或使用相機(jī)拍照。
(三)、效果圖
效果如下:
(四)、代碼展示
WXML頁(yè)面:
<view class="wrap"> <view class="contant_wrap"> <view class="contant"> <textarea name="bindTextAreaBlur" bindblur="bindTextAreaBlur" auto-height placeholder="請(qǐng)描述您的問(wèn)題或意見(jiàn)(必填)" maxlength="600"/> </view> <view class="contant-pic"> <view class="pics-list" wx:for="{{pics}}" wx:key="" > <image src="{{item}}" class="uploadImg"></image> <image src="../../images/delete.png" class="uploadImg-error" data-img="{{item}}" bindtap='deleteImg'></image> </view> <image src="../../images/uploadImg.png" class="uploadImg {{isShow?'true':'hideTrue'}}" bindtap='uploadImage' ></image> </view> </view> <view class="phone"> <input name="inputPhone" bindinput="inputPhone" placeholder="您的手機(jī)號(hào)或者郵箱(選填)" /> </view> <view class="phone"> <input name="inputName" bindinput="inputName" placeholder="您的稱呼(選填)" /> </view> <view class="bottom" bindtap='submitAdvice'> 保存</view> </view>
wxss頁(yè)面:
page{ background-color: #efefef; } .wrap{ width:90%; margin-left:5%; margin-top:10px; font-size:15px; } .contant_wrap{ background-color: #fff; } .contant{ width: 94%; margin: 0 auto } textarea{ min-height:300rpx; max-height: 300rpx; padding: 10rpx 0; width: 100%; } .contant-pic{ width: 94%; margin: 0 auto; height:80px; } .pics-list{ width:73px; height:73px; float:left; margin-right:6px; } .uploadImg{ width:70px; height:70px; } .uploadImg-error{ height:25px; width:25px; position:relative; top:-80px; left:55px; } .hideTrue { display: none } .true { display: block } input{ margin-top: 30rpx; height: 80rpx; padding-left: 20rpx; background-color: #fff; } .placeholder{ color: #999999; font-size: 12pt; } .bottom{ width:100%; height:40px; background-color:#e64340; position:fixed; bottom:0; color:#ffff; text-align: center; line-height: 40px; }
js中:
// pages/advice/advice.js Page({ /** 頁(yè)面的初始數(shù)據(jù)*/ data: { content:'', phone:'', name:'', advice:'', pics:[], isShow: true }, /**獲取textarea值:評(píng)論內(nèi)容 */ bindTextAreaBlur:function(e){ this.setData({ advice:e.detail.value }) }, /**獲取手機(jī)或郵箱*/ inputPhone: function (e) { this.setData({ phone: e.detail.value }) }, /**獲取稱呼 */ inputName: function (e) { this.setData({ name: e.detail.value }) }, /**上傳圖片 */ uploadImage:function(){ let that=this; let pics = that.data.pics; wx.chooseImage({ count:3 - pics.length, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: function(res) { let imgSrc = res.tempFilePaths; pics.push(imgSrc); if (pics.length >= 3){ that.setData({ isShow: false }) } that.setData({ pics: pics }) }, }) }, /**刪除圖片 */ deleteImg:function(e){ let that=this; let deleteImg=e.currentTarget.dataset.img; let pics = that.data.pics; let newPics=[]; for (let i = 0;i<pics.length; i++){ //判斷字符串是否相等 if (pics[i]["0"] !== deleteImg["0"]){ newPics.push(pics[i]) } } that.setData({ pics: newPics, isShow: true }) }, /**提交 */ submitAdvice:function(){ let that=this; let advice = that.data.advice let name=this.data.name let phone=this.data.phone let pics=this.data.pics //保存操作 } })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于JS中的全等和不全等、等于和不等于問(wèn)題
等號(hào)和非等號(hào)的同類運(yùn)算符是全等號(hào)和非全等號(hào)。這兩個(gè)運(yùn)算符所做的與等號(hào)和非等號(hào)相同,只是它們?cè)跈z查相等性前,不執(zhí)行類型轉(zhuǎn)換。接下來(lái)通過(guò)本文給大家介紹JS中的全等和不全等、等于和不等于,一起看看吧2021-09-09TypeScript里string和String的區(qū)別
這篇文章主要介紹了TypeScript里string和String的區(qū)別,真的不止是大小寫的區(qū)別,string表示原生類型,而String表示對(duì)象,下文更多詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03Swiper 4.x 使用方法(移動(dòng)端網(wǎng)站的內(nèi)容觸摸滑動(dòng))
Swiper是純javascript打造的滑動(dòng)特效插件,面向手機(jī)、平板電腦等移動(dòng)終端,這里為大家簡(jiǎn)單介紹一下Swiper4的用法,需要的朋友可以參考下2018-05-05基于HTML+JS實(shí)現(xiàn)網(wǎng)頁(yè)版蘋果計(jì)算器
這篇文章主要為大家詳細(xì)介紹了如何利用HTML+CSS+JS實(shí)現(xiàn)網(wǎng)頁(yè)版的蘋果計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06