微信小程序选择图片控件


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 相关文章推荐
jQuery帮助之CSS尺寸(五)outerHeight、outerWidth
Nov 14 Javascript
jQuery实现仿美橙互联两级导航菜单的方法
Mar 09 Javascript
JS基于Ajax实现的网页Loading效果代码
Oct 27 Javascript
JavaScript实现窗口抖动效果
Oct 19 Javascript
详解Vue.js 2.0 如何使用axios
Apr 21 Javascript
解决微信二次分享不显示摘要和图片的问题
Aug 18 Javascript
详解Vue实战指南之依赖注入(provide/inject)
Nov 13 Javascript
Layui实现数据表格默认全部显示(不要分页)
Oct 26 Javascript
详解vue-router 动态路由下子页面多页共活的解决方案
Dec 22 Javascript
Vue实现附件上传功能
May 28 Javascript
2020京东618叠蛋糕js脚本(亲测好用)
Jun 02 Javascript
jQuery中getJSON跨域原理的深入讲解
Sep 02 jQuery
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
xajax写的留言本
2006/11/25 PHP
人尽可用的Windows技巧小贴士之下篇
2007/03/22 PHP
在任意字符集下正常显示网页的方法一
2007/04/01 PHP
PHP和Shell实现检查SAMBA与NFS Server是否存在
2015/01/07 PHP
Laravel SQL语句记录方式(推荐)
2016/05/26 PHP
Open and Print a Word Document
2007/06/15 Javascript
手机平板等移动端适配跳转URL的js代码
2014/01/25 Javascript
倒记时60刷新网页的js代码
2014/02/18 Javascript
Angular的自定义指令以及实例
2016/12/26 Javascript
js实现下一页页码效果
2017/03/07 Javascript
jQuery EasyUI之验证框validatebox实例详解
2017/04/10 jQuery
详解webpack分包及异步加载套路
2017/06/29 Javascript
JavaScript 扩展运算符用法实例小结【基于ES6】
2019/06/17 Javascript
如何在node环境实现“get数据解析”代码实例
2020/07/03 Javascript
swiper4实现移动端导航栏tab滑动切换
2020/10/16 Javascript
[45:25]OG vs EG 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.22
2019/09/05 DOTA
Django 导出 Excel 代码的实例详解
2017/08/11 Python
python实现贪吃蛇游戏
2020/03/21 Python
mac系统下Redis安装和使用步骤详解
2019/07/09 Python
关于Keras模型可视化教程及关键问题的解决
2020/01/24 Python
django执行原始查询sql,并返回Dict字典例子
2020/04/01 Python
Python中logger日志模块详解
2020/08/04 Python
python生成word合同的实例方法
2021/01/12 Python
CSS3实现全景图特效示例代码
2018/03/26 HTML / CSS
纽约家具、家居装饰和地毯店:ABC Carpet & Home
2017/06/21 全球购物
Watch Station官方网站:世界一流的手表和智能手表
2020/01/05 全球购物
DTD的含义以及作用
2014/01/26 面试题
应届毕业生自我鉴定范文
2013/12/27 职场文书
销售行政专员职责
2014/01/03 职场文书
思想汇报格式
2014/01/05 职场文书
技能比赛获奖感言
2014/02/14 职场文书
群教班子对照检查材料
2014/08/26 职场文书
2014年最新学校运动会广播稿
2014/09/17 职场文书
群众路线教育实践活动学习笔记内容
2014/11/06 职场文书
基于angular实现树形二级表格
2021/10/16 Javascript
你知道Java Spring的两种事务吗
2022/03/16 Java/Android