我的需求是,用戶需要上傳圖片,上傳了一張圖片后崔拥,上傳框一直在圖片的右邊葱她,并根據(jù)圖片數(shù)量的增多昆箕,逐漸往右端移動。最多可以上傳6張圖片.圖片可以一張張刪除,也可以預(yù)覽,但是布局的時候還是踩了一個scroll-view的坑,接下來就把整個過程稍微講一下
1.沒有圖片的時候,上傳框是一直在中間的,上傳了一張圖片后,上傳框會在圖片,這里我先判斷存放圖片的那個數(shù)組imageList是不是為空,如果為空,上傳框居中,否則就加一個active類讓盒子不居中
.weui-uploader__input-box {
position: relative;
width: 169rpx;
height: 169rpx;
border: 1rpx solid #eaebeb;
border-radius: 8rpx;
background-color: #fff;
margin: 0 auto 18rpx;
}
.weui-uploader__input-box.active {
margin: 0 0 18rpx 15rpx;
}
2.上傳圖片就是調(diào)用小程序API了,這里可以去查閱文檔了,而且只能上傳非gif圖,這里我是調(diào)用的另外一個API判斷圖片信息,然后提示用戶只能上傳圖片允許格式為jpg,png,jpeg,能選擇6張,也是判斷數(shù)組里的圖片加上上傳的圖片總數(shù)是不是超過了,就提示只能選擇6張圖片
wx.chooseImage({
sourceType: ['camera', 'album'],
sizeType: ['compressed'],
count: this.data.count,
success: function (res) {
var tempList = res.tempFilePaths;
for (let i = 0; i < tempList.length;i++){
wx.getImageInfo({
src: tempList[i],
success(res) {
console.log(res);
kk++;
if(res.type == 'gif'){
tempList.splice(i, 1);
icom.alert('圖片允許格式為jpg,png,jpeg');
}
if(kk == tempList.length){
var currentArr = $page.data.imageList.concat(tempList);
that.setData({
imageList: currentArr,
count: $page.data.maxCount - (currentArr.length)
})
}
}
})
}
}
})
3.布局問題.我是左右兩個盒子flex布局.左邊是一個盒子包裹scroll-view標(biāo)簽.存放圖片,右邊是上傳框,scroll-view的寬度是calc布局,max-width: calc(100% - 180rpx);如果想橫向布局的話需要給scroll-view加上white-space: nowrap;屬性,子元素添加display: inline-block;不然就不會橫向滾動(很坑),但是還有一個問題是,安卓手機(jī)上傳超過3張時,圖片就會顯示不見,但是蘋果是好的,,,,這就很無語了,
然后我開始查文檔,都沒發(fā)現(xiàn)我布局有啥問題.后來無意中發(fā)現(xiàn)我給scroll-view加了一個高度,,,所以圖片就不行了,然后我把高度去掉,給他的父親加.就完美解決,,,耶.....
我的理解是,scroll-view縱向滾動時,必須加高度.橫向滾動時,不能加高度,加寬度就可以.
4.刪除圖片,也是寫一個移除事件,操作數(shù)組,我是拿到點擊的index,然后把index對應(yīng)的圖片移除掉,就可以解決了,.預(yù)覽圖片也是調(diào)的API,查文檔啦
以下是完整版本布局,有更好的解決方法,可以評論哦,記得點贊啦,謝謝
html
<!-- 上傳圖片 -->
<view class="weui-uploader">
<view class="weui-uploader__hd">
<view></view>
<view class="weui-uploader__info">{{imageList.length}}/{{maxCount}}</view>
</view>
<view class="weui-uploader__bd">
<view class="weui-uploader__files {{imageList.length>=maxCount ? 'active' : ''}}" wx-if="{{imageList.length>0}}">
<scroll-view class="scroll-view_w" scroll-x="true" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll">
<block wx:for="{{imageList}}" wx:for-item="image" wx:key="mark">
<view class="weui-uploader__file">
<image class="weui-uploader__img" src="{{image}}" mode='aspectFill'></image>
<view class='clean' bindtap='cleanpic' data-index="{{index}}"></view>
<view class='previewImage' data-src="{{image}}" bindtap="previewImage"></view>
</view>
</block>
</scroll-view>
</view>
<view class="weui-uploader__input-box {{imageList.length>0 ? 'active' : ''}}">
<view class="weui-uploader__input" bindtap="chooseImage"></view>
</view>
</view>
</view>
css
.weui-uploader__hd {
display: flex;
justify-content: space-between;
margin-bottom: 18rpx;
}
.weui-uploader__info {
color: #b2b2b2;
font-size: 24rpx;
}
.weui-uploader__bd {
overflow: hidden;
margin-bottom: -8rpx;
/* margin-right: -18rpx; */
width: 100%;
box-sizing: border-box;
display: flex;
}
.weui-uploader__files {
/* overflow: hidden; */
max-width: calc(100% - 180rpx);
height: 169rpx;
margin-bottom: 18rpx;
box-sizing: border-box;
}
.scroll-view_w {
width:100%;
white-space: nowrap;
text-align: right;
}
.weui-uploader__file {
display: inline-block;
max-width: 169rpx;
min-width: 169rpx;
width: 169rpx;
height: 169rpx;
border: 1rpx solid #eaebeb;
border-radius: 8rpx;
margin-right: 18rpx;
position: relative;
}
.weui-uploader__img {
width: 100%;
height: 100%;
object-fit: cover;
}
.weui-uploader__input-box {
position: relative;
width: 169rpx;
height: 169rpx;
border: 1rpx solid #eaebeb;
border-radius: 8rpx;
background-color: #fff;
/* float: right; */
margin: 0 auto 18rpx;
}
.weui-uploader__input-box.active {
margin: 0 0 18rpx 15rpx;
}
.weui-uploader__input-box:before {
content: " ";
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
width: 54rpx;
height: 41rpx;
background-image: url(https://clarins.beats-digital.com/wxapp/images/grassSharing/icon_camera.png?sign=c59d2f414254389e53a98b7fe83baeaf&t=15506442951);
background-position: 50% 50%;
background-repeat: no-repeat;
background-size: 100% 100%;
background-origin: padding-box;
background-clip: padding-box;
}
.weui-uploader__input-box:active {
border-color: #d9d9d9;
}
.weui-uploader__input-box:active:before {
opacity: 0.8;
}
.weui-uploader__input {
position: absolute;
z-index: 1;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
.previewImage{
width: 100%;
height: calc(100% - 30rpx);
position: absolute;
top: 30rpx;
left: 0;
}
.clean {
width: 30rpx;
height: 30rpx;
position: absolute;
top: 0;
right: 0;
background-color: rgba(238, 238, 238, 0.3);
transform: rotate(45deg);
border-radius: 50%;
}
.clean::after, .clean::before {
content: '';
position: absolute;
background-color: red;
transform-origin: top;
}
.clean::after {
width: 30rpx;
height: 4rpx;
top: 50%;
left: 0;
transform: translate(0, -50%);
}
.clean::before {
width: 4rpx;
height: 30rpx;
top: 0;
left: 50%;
transform: translate(-50%, 0);
}
js
let PageData = {
imageList: [],
maxCount:6,
count: 6,
};
Page({
data: Object.assign({}, PageData), //頁面的初始數(shù)據(jù)
onLoad: function (option) {
$page = this;
$query = option;
},
onReady: function () { }, //監(jiān)聽頁面初次渲染完成
onShow: function () {}, //監(jiān)聽頁面顯示
onHide: function () { }, //監(jiān)聽頁面隱藏
onUnload: function () { }, //監(jiān)聽頁面卸載
onPullDownRefresh: function () { }, //頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
onReachBottom: function () { }, //頁面上拉觸底事件的處理函數(shù)
onShareAppMessage: function () { },//用戶點擊右上角分享
chooseImage: function () {
var that = this;
let kk = 0;
if(that.data.imageList.length == that.data.maxCount){
//圖片已達(dá)上線
wx.showModal({
content: '你最多只能選擇' + that.data.maxCount+'張照片',
confirmText: '我知道了',
showCancel: false,
success(res) {
if (res.confirm) {
console.log('用戶點擊我知道了')
} else if (res.cancel) {
}
}
})
return
}
wx.chooseImage({
sourceType: ['camera', 'album'],
sizeType: ['compressed'],
count: this.data.count,
success: function (res) {
var tempList = res.tempFilePaths;
for (let i = 0; i < tempList.length;i++){
wx.getImageInfo({
src: tempList[i],
success(res) {
console.log(res);
kk++;
if(res.type == 'gif'){
tempList.splice(i, 1);
icom.alert('圖片允許格式為jpg,png,jpeg');
}
if(kk == tempList.length){
var currentArr = $page.data.imageList.concat(tempList);
that.setData({
imageList: currentArr,
count: $page.data.maxCount - (currentArr.length)
})
}
}
})
}
}
})
},
previewImage: function (e) {
var current = e.target.dataset.src
wx.previewImage({
current: current,
urls: this.data.imageList
})
},
cleanpic: function (e) {
//點擊清除按鈕
var index = parseInt(e.target.dataset.index);
$page.data.imageList.splice(index, 1);
$page.setData({
imageList: $page.data.imageList,
count: $page.data.maxCount - ($page.data.imageList.length)
})
},
}) //end page