自定义input上传图片框

ro1YfU.png

<!-- 自定义按钮效果 -->
                <div class="input_img_father_div">
                    <input class="upload_input" id="result_background"  name="result_background"  type="file"  />
                    <div class="input_img_btn_div">
                        <c:if test="${empty result.result_background}"> <span class="input_img_btn_span">上传图片:</span></c:if>
                        <img id="img_result_background" class="upload" style=" width:80px; height:80px;"  src="${result.result_background}" />
                    </div>
                </div>
                <!-- 自定义按钮效果 -->
// {# 上传样板图片预览 #}
    $(".upload_input").change(function () {
        var objUrl = getObjectURL(this.files[0]);//获取文件信息
        console.log(objUrl)
        if (objUrl) {
            $(this).parent().find('span.input_img_btn_span').hide();
            $(this).parent().find('img.upload').show();
            $(this).parent().find("img.upload").attr("src", objUrl);
        }
    });
    function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL != undefined) {
            url = window.createObjectURL(file);
        } else if (window.URL != undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
/****上传图片****/
.input_img_father_div {
    position: relative;
    width: 80px;
    height: 80px;
    vertical-align: middle;
    opacity: 60;
    background-color: #c6c6c6;
    display: flex; /*flex弹性布局*/
    justify-content: center;
    align-items: center;
    margin-left: 137px;
}
.upload_input {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: 0;
    width: 80px;
    height: 80px;
}

.input_img_btn_div {
    text-align: center;
    display: flex;
    flex-direction: column; /*元素的排列方向为垂直*/
    justify-content: center; /*水平居中对齐*/
    align-items: center; /*垂直居中对齐*/
}

.input_img_btn_span {
    font-size: 16px;
    vertical-align: middle;
}
img.upload {
    width: 80px;
    height: 80px;
}
/****上传图片****/
posted @ 2020-12-28 10:25  748573200000  阅读(272)  评论(0编辑  收藏  举报