js实现上传头像(看了你就懂,相信我)
效果图:
html:
<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:
.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:
$(".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
分类:
前端
标签:
javascript
, 前端
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?