小程序的图片选择控件怎样实现?
Admin 2021-05-07 群英技术资讯 1372 次浏览
微信小程序的图片选择控件怎样实现?在使用微信小程序上传图片时,一般我们都需要用到图片选择控件,这样我们能比较便捷的从手机本地相差将图片上传到微信上,那么小程序的图片选择控件效果是如何实现的呢?下面我们一起来学习一下。

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, 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
});
},
// 删除照片 &&
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
})
},
})
以上就是关于小程序的图片选择控件如何实现的介绍,上述代码有一定的参考价值,有需要的朋友可以看一看,希望对大家学习有帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了解决Vue大括号字符换行踩的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
Angular指令如何保持关注点的分离?本篇文章带大家了解一下通过 Angular 指令保持关注点分离的方法,希望对大家有所帮助!
JavaScript中求数组平均数的思路和方法是什么?对于求平均数,大家都很熟悉,那么对与不了解数组个数的求平均数,该怎样用代码来实现呢?下文给大家介绍一个方法,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
这篇文章主要给大家分享的是Vue3的几个优势,Vue3仍然在源码、性能和语法 API 三个大的方面进行了优化,下面我们一起进入文章看看具体详情吧
这篇文章主要为大家详细介绍了js实现轮播图自动切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008