微信小程序选择图片控件


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:将其它类型值转换成布尔类型值的解决方法详解
May 07 Javascript
Javascript变量的作用域和作用域链详解
Apr 02 Javascript
使用AOP改善javascript代码
May 01 Javascript
bootstrap vue.js实现tab效果
Feb 07 Javascript
jQuery按需加载轮播图(web前端性能优化)
Feb 17 Javascript
JS实现移动端实时监听输入框变化的实例代码
Apr 12 Javascript
详解如何让Express支持async/await
Oct 09 Javascript
使用vue制作探探滑动堆叠组件的实例代码
Mar 07 Javascript
解决vue 打包发布去#和页面空白的问题
Sep 04 Javascript
原生JavaScript实现的无缝滚动功能详解
Jan 17 Javascript
JavaScript禁止右击保存图片,禁止拖拽图片的实现代码
Apr 28 Javascript
vue中解决拖拽改变存在iframe的div大小时卡顿问题
Jul 22 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 极验验证码实例讲解
2016/09/29 PHP
自写的利用PDO对mysql数据库增删改查操作类
2018/02/19 PHP
PHP加MySQL消息队列深入理解
2021/02/27 PHP
ExtJS4 表格的嵌套 rowExpander应用
2014/05/02 Javascript
jQuery制作简单柱状图实例
2015/01/28 Javascript
Jquery幻灯片特效代码分享--打开页面随机选择切换方式(3)
2015/08/15 Javascript
JS实现回到页面顶部动画效果的简单实例
2016/05/24 Javascript
AngularJS入门教程之控制器详解
2016/07/27 Javascript
Bootstrap中的fileinput 多图片上传及编辑功能
2016/09/05 Javascript
jQuery密码强度验证控件使用详解
2017/01/05 Javascript
JavaScript定义全局对象的方法示例
2017/01/12 Javascript
jQuery之动画ajax事件(实例讲解)
2017/07/18 jQuery
详解vue-cli构建项目反向代理配置
2017/09/07 Javascript
解决Vue中引入swiper,在数据渲染的时候,发生不滑动的问题
2018/09/27 Javascript
vue路由传参三种基本方式详解
2019/12/09 Javascript
微信小程序以ssm做后台开发的实现示例
2020/04/08 Javascript
python 获取et和excel的版本号
2009/04/09 Python
Python实现的Google IP 可用性检测脚本
2015/04/23 Python
python字符串对其居中显示的方法
2015/07/11 Python
sublime text 3配置使用python操作方法
2017/06/11 Python
numpy中以文本的方式存储以及读取数据方法
2018/06/04 Python
解决jupyter notebook import error但是命令提示符import正常的问题
2020/04/15 Python
如何在django中运行scrapy框架
2020/04/22 Python
Html5新标签datalist实现输入框与后台数据库数据的动态匹配
2017/05/18 HTML / CSS
土木工程实习生自我鉴定
2013/09/19 职场文书
简历中求职的个人自我评价
2013/12/03 职场文书
创建服务型党组织实施方案
2014/02/25 职场文书
大学活动总结格式
2014/04/29 职场文书
个人承诺书怎么写
2014/05/24 职场文书
材料专业大学毕业生自荐书
2014/07/02 职场文书
金融专业求职信
2014/08/05 职场文书
小学家长学校培训材料
2014/08/24 职场文书
2014年全国爱牙日宣传活动方案
2014/09/21 职场文书
祖国在我心中演讲稿600字
2014/09/23 职场文书
七年级上册生物的课件
2019/08/07 职场文书
Python使用永中文档转换服务
2022/05/06 Python