前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js实现上传头像(看了你就懂,相信我)

js实现上传头像(看了你就懂,相信我)

作者头像
马克社区
发布2022-07-18 20:17:27
4510
发布2022-07-18 20:17:27
举报
文章被收录于专栏:高端IT高端IT

效果图:

在这里插入图片描述
在这里插入图片描述

html:

代码语言:javascript
复制
<div class="tx ovf">
        <div class="tit">头像</div>
        <div class="ovf tx-img"><img src="" alt="">
            <div class="upload-b"><span>修改头像</span><input type="file" class="upload1"></div>
        </div>
    </div>

css:

代码语言:javascript
复制
 .ovf{overflow: hidden;}
    .tit { float: left; width: 50px; margin-top: 39px; font-size: 0.9vw; color: #999999; }
    .tx{ margin-top:44px; }
    .tx img{ width: 90px; height: 90px; border-radius:50%; margin-right:30px;border: 1px solid #fca89d; }
    .tx span{ border: solid 1px #fca89d; color: #fca89d; font-size: 16px; padding: 10px; cursor: pointer; }
    .upload1 { position: absolute; right:0; }
    .tx img{ float: left; }
    .tx input{ width: 86%; opacity: 0; }
    .upload-b { float: left; margin-top:36px; position: relative; }

js:

代码语言:javascript
复制
$(".upload1").change(function(){
        var file=this.files[0];
        readFile(file);
        });
    function readFile(file) {
        // 新建阅读器
        var reader = new FileReader();
        // 根据文件类型选择阅读方式
        switch (file.type){
        case 'image/jpg':
        case 'image/png':
        case 'image/jpeg':
        case 'image/gif':
        reader.readAsDataURL(file);

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120074416

本文系转载,前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com