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

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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
用JS怎样做拖动div交换位置的效果?也就是在多个div中,我们可以拖动任意一个div,实现位置交换的效果,那么这一效果是如何实现的呢?接下来我们就一起来了解一下实现原理,下面还有实现代码及效果,感兴趣的朋友可以参考。
小程序怎样实现分页查询列表的模板,及代码是什么?下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
这篇文章主要为大家详细介绍了JavaScript实现div的鼠标拖拽效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要给大家分享使用JS做窗口滚动效果的内容,小编觉得挺有意思的,因此分享给大家做个参考,实现效果和代码如下,感兴趣的朋友可以参考,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
这篇文章主要介绍了前端的状态管理,续上篇文章内容,今天将从 Redux 入手逐渐拓展,需要的小伙伴可以参考一下哟
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008