微信小程序选择图片控件


Posted in Javascript onJanuary 19, 2021

本文实例为大家分享了微信小程序选择图片控件的具体代码,供大家参考,具体内容如下

微信小程序选择图片控件

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
 })
 },
 
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript实现划词标记+划词搜索功能
Mar 06 Javascript
深入理解javascript中return的作用
Dec 30 Javascript
JavaScript中的Math.LN2属性用法详解
Jun 12 Javascript
Angularjs中的页面访问权限怎么设置
Nov 11 Javascript
canvas 绘制圆形时钟
Feb 22 Javascript
微信公众号菜单配置微信小程序实例详解
Mar 31 Javascript
Vue实现内部组件轮播切换效果的示例代码
Apr 07 Javascript
详解给Vue2路由导航钩子和axios拦截器做个封装
Apr 10 Javascript
Vue.js上传图片到阿里云OSS存储的方法示例
Dec 13 Javascript
Vue-Cli 3.0 中配置高德地图的两种方式
Jun 19 Javascript
基于layui的下拉列表的数据回显方法
Sep 24 Javascript
webpack3.0升级4.0的方法步骤
Apr 02 Javascript
jQuery冲突问题解决方法
Jan 19 #jQuery
js实现随机点名
Jan 19 #Javascript
js实现有趣的倒计时效果
Jan 19 #Javascript
微信小程序之高德地图多点路线规划过程示例详解
Jan 18 #Javascript
从源码角度来回答keep-alive组件的缓存原理
Jan 18 #Javascript
在JavaScript中查找字符串中最长单词的三种方法(推荐)
Jan 18 #Javascript
vue-resource 拦截器interceptors使用详解
Jan 18 #Vue.js
You might like
php中配置文件操作 如config.php文件的读取修改等操作
2012/07/07 PHP
php的hash算法介绍
2014/02/13 PHP
php使用smtp发送支持附件的邮件示例
2014/04/13 PHP
PHP和Shell实现检查SAMBA与NFS Server是否存在
2015/01/07 PHP
php简单创建zip压缩文件的方法
2016/04/30 PHP
JavaScript 入门·JavaScript 具有全范围的运算符
2007/10/01 Javascript
jQuery AJAX 调用WebService实现代码
2010/03/24 Javascript
javascript cookies操作集合
2010/04/12 Javascript
javascript从image转换为base64位编码的String
2014/07/29 Javascript
用瀑布流的方式在网页上插入图片的简单实现方法
2016/09/23 Javascript
javascript中setAttribute兼容性用法分析
2016/12/12 Javascript
JS填写银行卡号每隔4位数字加一个空格
2016/12/19 Javascript
js将字符串中的每一个单词的首字母变为大写其余均为小写
2017/01/05 Javascript
python机器学习之决策树分类详解
2017/12/20 Python
opencv python 基于KNN的手写体识别的实例
2018/08/03 Python
Python3 使用pillow库生成随机验证码
2019/08/26 Python
阿里云ECS服务器部署django的方法
2019/08/29 Python
Python hashlib模块加密过程解析
2019/11/05 Python
Python imutils 填充图片周边为黑色的实现
2020/01/19 Python
Python环境下安装PyGame和PyOpenGL的方法
2020/03/25 Python
Python collections模块的使用方法
2020/10/09 Python
雅诗兰黛美国官网:Estee Lauder美国
2016/07/21 全球购物
Zalando Lounge瑞士:时尚与生活方式购物俱乐部
2020/03/12 全球购物
运动会获奖感言
2014/02/11 职场文书
电气自动化专业职业规划范文
2014/02/16 职场文书
知识竞赛活动方案
2014/02/18 职场文书
国培计划培训感言
2014/03/11 职场文书
大二学习计划书范文
2014/04/27 职场文书
师德师风建设整改措施思想汇报
2014/10/11 职场文书
2014年稽查工作总结
2014/12/20 职场文书
先进工作者推荐材料
2014/12/23 职场文书
幼儿园教师个人总结
2015/02/05 职场文书
个人年终总结开头
2015/03/06 职场文书
2015中学政教处工作总结
2015/07/22 职场文书
JAVA长虹键法之建造者Builder模式实现
2022/04/10 Java/Android
Win10加载疑难解答时出错发生意外错误的解决方法
2022/07/07 数码科技