1. 程式人生 > 程式設計 >微信小程式選擇圖片控制元件

微信小程式選擇圖片控制元件

本文例項為大家分享了微信小程式選擇圖片控制元件的具體程式碼,供大家參考,具體內容如下

微信小程式選擇圖片控制元件

xml:

<loading hidden="{{loadingHidden}}">
 載入中...
</loading>
<view class="add_carimg">
 <block>
 <view class="load_iamge">
 <text class="load_head_text">上傳施工車輛照片</text>
 <text class="load_foot_text">{{imgbox.length}}/2</text>
 </view>
 <view class='pages'>
 <view class="images_box">
 <block wx:key="imgbox" wx:for="{{imgbox}}">
  <view class='img-box'>
  <image class='img' src='{{item}}' data-message="{{item}}" bindtap="imgYu"></image>
  <view class='img-delect' data-deindex='{{index}}' bindtap='imgDelete1'>
  <image class='img' src='/pages/images/delete_btn.png'></image>
  </view>
  </view>
 </block>
 <view class='img-box' bindtap='addPic1' wx:if="{{imgbox.length<2}}">
  <image class='img' src='/pages/images/load_image.png'></image>
 </view>
 </view>
 </view>
 </block>
</view>
<view>
 <button class="work_btn" bindtap="shanggang">上崗</button>
</view>

css:

.work_btn {
 width: 60%;
 height: 35px;
 line-height: 35px;
 margin-top: 15px;
 border-radius: 5px;
 font-size: 30rpx;
 color: white;
 background-color: rgb(2,218,247);
}
 
.work_btn:active {
 width: 60%;
 height: 35px;
 line-height: 35px;
 margin-top: 15px;
 border-radius: 5px;
 font-size: 30rpx;
 color: white;
 background-color: rgb(151,222,231);
}
 
/*********/
 
.load_iamge {
 width: 100%;
 height: 30px;
 margin-top: 10px;
 display: flex;
 flex-direction: row;
}
 
.load_head_text {
 width: 95%;
 height: 20px;
 margin-bottom: 5px;
 margin-top: 5px;
 
 
}
 
.load_foot_text {
 width: 5%;
 height: 20px;
 margin-right: 15px;
 margin-top: 5px;
 margin-bottom: 5px;
 float: right;
 
}
 
.pages {
 width: 98%;
 margin: auto;
 overflow: hidden;
}
 
/* 圖片 */
.images_box {
 width: 100%;
 display: flex;
 flex-direction: row;
 flex-wrap: wrap;
 justify-content: flex-start;
 background-color: white;
}
 
.img-box {
 border: 2rpx;
 border-style: solid;
 border-color: rgba(170,167,0.452);
 width: 200rpx;
 height: 200rpx;
 margin-left: 35rpx;
 margin-top: 20rpx;
 margin-bottom: 20rpx;
 position: relative;
}
 
/* 刪除圖片 */
.img-delect {
 width: 50rpx;
 height: 50rpx;
 border-radius: 50%;
 position: absolute;
 right: -20rpx;
 top: -20rpx;
}
 
.img {
 width: 100%;
 height: 100%;
}

js:

Page({
 
 /**
 * 頁面的初始資料
 */
 data: {
 tempFilePaths: '',imgbox: [],//選擇圖片
 fileIDs: [],//上傳雲端儲存後的返回值
 src: 0,},onLoad: function (options) {
 
 
 },//圖片點選事件
 imgYu: function (event) {
 var that = this;
 
 console.log(event.target.dataset.message + "這是啥");
 var src = event.target.dataset.message;
 //圖片預覽
 wx.previewImage({
 current: src,// 當前顯示圖片的http連結
 urls: [src] // 需要預覽的圖片http連結列表
 })
 },// 刪除照片 &&
 imgDelete1: function (e) {
 let that = this;
 let index = e.currentTarget.dataset.deindex;
 let imgbox = this.data.imgbox;
 imgbox.splice(index,1)
 that.setData({
 imgbox: imgbox
 });
 },// 選擇圖片 &&&
 addPic1: function (e) {
 var imgbox = this.data.imgbox;
 console.log(imgbox)
 var that = this;
 var n = 2;
 if (2 > imgbox.length > 0) {
 n = 2 - imgbox.length;
 } else if (imgbox.length == 2) {
 n = 1;
 }
 wx.chooseImage({
 count: n,// 預設9,設定圖片張數
 sizeType: ['original','compressed'],// 可以指定是原圖還是壓縮圖,預設二者都有
 sourceType: ['album','camera'],// 可以指定來源是相簿還是相機,預設二者都有
 success: function (res) {
  // console.log(res.tempFilePaths)
  // 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片
  var tempFilePaths = res.tempFilePaths
  console.log('路徑' + tempFilePaths);
  if (imgbox.length == 0) {
  imgbox = tempFilePaths
  } else if (2 > imgbox.length) {
  imgbox = imgbox.concat(tempFilePaths);
  }
  that.setData({
  imgbox: imgbox,imgnum: imgbox.length
  });
 }
 })
 },//圖片
 imgbox: function (e) {
 this.setData({
 imgbox: e.detail.value
 })
 },})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。