Html 图片上传

HTML

<div class="gallery" id="gallery">
                                <span class="gallery__img" id="galleryImg"></span>
                                <div class="gallery__opr">
                                    <a href="javascript:" class="gallery__del">
                                        <img src="/Content/Sys/del.svg" />
                                    </a>
                                </div>
                            </div>
                            <div class="uploader">
                                <div class="uploader__hd">
                                    <p class="uploader__title">轮播图片</p>
                                    <div class="uploader__info">0/2</div>
                                </div>
                                <div class="uploader__bd">
                                    <ul class="uploader__files" id="uploaderFiles">
                                        <li class="uploader__file uploader__file_status"
                                            style="background-image: url(/Content/Sys/upload.svg)">
                                            <div class="uploader__file-content">50%</div>
                                        </li>
                                    </ul>
                                    <div class="uploader__input-box">
                                        <input id="uploaderInput" class="uploader__input" type="file" accept="image/*" multiple />
                                    </div>
                                </div>
                            </div>

CSS

.uploader__hd {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-align-items: center;
    align-items: center;
    position: relative;
}

.uploader__title {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    font-family: 'Microsoft YaHei UI';
    font-size: 14px;
}

.uploader__info {
    color: #B2B2B2;
    position: absolute;
    right: 0;
}

.uploader__bd {
    margin-bottom: -4px;
    margin-right: -9px;
    overflow: hidden;
}

.uploader__files {
    list-style: none;
}

.uploader__file {
    float: left;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 79px;
    height: 79px;
    background: no-repeat center center;
    background-size: cover;
}

.uploader__file_status {
    position: relative;
}

    .uploader__file_status:before {
        content: " ";
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-color: rgba(0, 0, 0, 0.5);
    }

    .uploader__file_status .uploader__file-content {
        display: block;
    }

.uploader__file-content {
    display: none;
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    color: #FFFFFF;
}

    .uploader__file-content .icon-warn {
        display: inline-block;
    }

.uploader__input-box {
    float: left;
    position: relative;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 77px;
    height: 77px;
    border: 1px solid #D9D9D9;
}

    .uploader__input-box:before,
    .uploader__input-box:after {
        content: " ";
        position: absolute;
        top: 50%;
        left: 50%;
        -webkit-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
        background-color: #D9D9D9;
    }

    .uploader__input-box:before {
        width: 2px;
        height: 39.5px;
    }

    .uploader__input-box:after {
        width: 39.5px;
        height: 2px;
    }

    .uploader__input-box:active {
        border-color: #999999;
    }

        .uploader__input-box:active:before,
        .uploader__input-box:active:after {
            background-color: #999999;
        }

.uploader__input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}


/*预览*/

.gallery {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: #000000;
    z-index: 1000;
}

.gallery__img {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 60px;
    left: 0;
    background: center center no-repeat;
    background-size: contain;
}

.gallery__opr {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: #0D0D0D;
    color: #FFFFFF;
    line-height: 60px;
    text-align: center;
}

.gallery__del {
    display: block;
}

JS

$(function () {
    var tmpl = '<li class="uploader__file" style="background-image:url(#url#)"></li>',
        $gallery = $("#gallery"), $galleryImg = $("#galleryImg"),
        $uploaderInput = $("#uploaderInput"),
        $uploaderFiles = $("#uploaderFiles")
    ;

    $uploaderInput.on("change", function (e) {
        var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];

            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }

            $uploaderFiles.append($(tmpl.replace('#url#', src)));
        }
    });
    $uploaderFiles.on("click", "li", function () {
        $galleryImg.attr("style", this.getAttribute("style"));
        $gallery.fadeIn(100);
    });
    $gallery.on("click", function () {
        $gallery.fadeOut(100);
    });
});
原文地址:https://www.cnblogs.com/sky-gfan/p/8434959.html