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

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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是有关微信小程序实现切换页面的内容。小编觉得挺实用的,因此分享给大家做个参考,这里要实现的需求是,点击导航条可以切换部分页面,具体的实现效果如下,感兴趣的朋友就继续往下看吧。
目录一、input的校验没有可说的,记住俩点,一个控制输入长度,一个控制格式。二、验证码逻辑:组件使用的是vant ui,具体用法可去官网看。分几个部分考虑,一、输入框input的校验:1、blur时没有值和格式不符合的逻辑校验2、限制输入长度逻辑,比如手机号只能11位,验证码只能6位。二、验证码按钮逻辑:1、不同状态
这篇文章主要为大家介绍了JavaScript之Array常见的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助V
这篇文章主要介绍了Vue slot插槽的使用,在生活中很多地方都有插槽,电脑usb的插槽,插板当中的电源插槽,插槽的目的是为了让我们原来的设备具备更多的扩展性比如电脑的USB我们可以插入U盘,手机,鼠标,键盘等等,下面文章就来介绍Vue slot插槽是如何使用的
在javascript中,可以使用Number对象的toString()方法来将数字转为Hex(16进制),该方法可以解析指定数值,并尝试返回指定基数(进制)的字符串表示形式,语法为“指定数字对象.toString(16);”。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008