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); }); });
分类:
前端
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律