自定義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;
}
/****上傳圖片****/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章