js实现头像上传(移动端,PC端均可)

html

 1     <div class="parent">
 2             <img src="" id="portrait" /> //显示用户所选到的图片,即预览
 3             <div class="child">  //点击区域     
 4                  <svg class="icon" aria-hidden="true">
 5                      <use xlink:href="#icon-shangchuantupiananniu_moren"></use>
 6                   </svg>
 7                    <input type="file" id="file" accept="image/*"  multiple="multiple"/><br />  //文件上传控件
 8              </div>
 9        </div>
10        <input type="hidden" value="" id="baseImg" />  //保存转成abse64的图片
11        <button class="submitImg mui-btn-blue" type="button">上传</button>  //上传按钮

css

 1 #portrait {
 2         width: 79px;  //限制所选图片的大小 以防图片过大手机显示不下
 3         height: auto;
 4 }
 5 .submitImg {
 6         margin: 50px auto;
 7         display: block;
 8 }
 9             
10 .child {
11         position: relative;
12         display: inline-block;
13 }
14             
15 #file {
16         position: absolute;
17         left: 0px;
18         top: 0px;
19         opacity: 0;
20         height: 100%;
21         width: 100%;
22 }
23             

js

//图片上传
mui('#wrap').on('change', '#file', function() {
        var obj = this.files[0];
        var fr = new FileReader();
        fr.onload = function() {
            mui("#portrait")[0].setAttribute('src', this.result);
            mui('#baseImg')[0].value = this.result;
        };
        fr.readAsDataURL(obj);

})

 

转载于:https://www.cnblogs.com/susutong/p/10001261.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值