关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

CSS之自定义文件上传

发布时间:2019-12-24 16:48:36

思路:点击label元素与点击input元素有相同的效果,因此隐藏input即可。

代码
HTML:
<label for="file" class="file-label">
    <span class="label-placeholder">文件上传</span>
    <img src="" alt="" id="img">
</label>
<input type="file" id="file" class="file-input">


javascript(用了jquery):
$("#file").on("change",function(event){
    $("#img").attr("src",URL.createObjectURL(event.target.files[0]));
})


CSS:
.file-input{
    display:none;
}
.file-label{
    width:5rem;
    height:5rem;
    border-radius: 50%;
    border:solid 1px gray;
    background-color: #eee;
    display:flex;
    align-items: center;
    justify-content: center;
    position: relative;
}
#img{
    width:100%;
    height: 100%;
    border-radius: 50%;
    object-fit:cover;
    z-index: 1;
}
.label-placeholder{
    position:absolute;
}


/template/Home/Zkeys/PC/Static