这次给大家带来H5怎样做出图片拖拽上传预览组件,H5做出图片拖拽上传预览组件的注意事项有哪些,下面就是实战案例,一起来看一下。
H5中拖拽事件有:
[ - ] DragDrop:拖放完成,也就是鼠标拖入对象并在拖放区域释放。
[ - ] DragEnter :拖放进入,也就是鼠标拖放对象进入拖放区域。
[ - ] DragLeave:离开拖放区域。
[ - ] DragOver :拖放对象悬浮于拖放区域,在拖放区域内移动时多次触发。
1.拖拽文件获取文件信息
示例
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style> .example { padding: 10px; border: 1px solid #ccc; } #drop_zone { border: 2px dashed #bbb; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; padding: 25px; text-align: center; font: 20pt bold 'Vollkorn'; color: #bbb; } </style> </head> <body> <div class="example"> <div id="drop_zone">将文件拖放到此处</div> <output id="list"></output> </div> <script> function handleFileSelect(evt) { evt.stopPropagation(); //阻止默认事件 evt.preventDefault(); //阻止默认事件 var files = evt.dataTransfer.files;//获取文件集 var output = []; for(var i = 0, f; f = files[i]; i++) { output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate.toLocaleDateString(), '</li>'); } document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; } function handleDragOver(evt) { evt.stopPropagation(); evt.preventDefault(); evt.dataTransfer.dropEffect = 'copy'; } var dropZone = document.getElementById('drop_zone'); dropZone.addEventListener('dragover', handleDragOver, false); dropZone.addEventListener('drop', handleFileSelect, false); //body中阻止 拖拽事件防止拖拽错误 document.body.addEventListener('dragover', function(evt) { evt.stopPropagation(); //阻止默认事件 evt.preventDefault(); //阻止默认事件 return false; }, false); document.body.addEventListener('drop', function(evt) { evt.stopPropagation(); //阻止默认事件 evt.preventDefault(); //阻止默认事件 return false; }, false); </script> </body> </html>
2.限制文件大小与文件格式
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style> .example { padding: 10px; border: 1px solid #ccc; } #drop_zone { border: 2px dashed #bbb; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; padding: 25px; text-align: center; font: 20pt bold 'Vollkorn'; color: #bbb; } </style> </head> <body> <div class="example"> <div id="drop_zone">将文件拖放到此处</div> <output id="list"></output> </div> <script> function handleFileSelect(evt) { evt.stopPropagation(); //阻止默认事件 evt.preventDefault(); //阻止默认事件 var files = evt.dataTransfer.files;//获取文件集 var output = []; for(var i = 0, f; f = files[i]; i++) { if(f.size<1024*1024*2&&(f.type=="image/png"||f.type=="image/jpeg")){//<===这里 output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate.toLocaleDateString(), '</li>'); } } document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>'; } function handleDragOver(evt) { evt.stopPropagation(); evt.preventDefault(); evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy. } var dropZone = document.getElementById('drop_zone'); dropZone.addEventListener('dragover', handleDragOver, false); dropZone.addEventListener('drop', handleFileSelect, false); //body中阻止 拖拽事件防止拖拽错误 document.body.addEventListener('dragover', function(evt) { evt.stopPropagation(); //阻止默认事件 evt.preventDefault(); //阻止默认事件 return false; }, false); document.body.addEventListener('drop', function(evt) { evt.stopPropagation(); //阻止默认事件 evt.preventDefault(); //阻止默认事件 return false; }, false); </script> </body> </html>
3.显示缩略图与动态增删效果
示例
<!doctype html><html> <head> <meta charset="UTF-8" /> <title>简易上传预览</title> <style type="text/css"> #drop_zone { display: block; border: 2px dashed #BBB; padding: 25px 5px; text-align: center; font-size: 20pt; color: #BBB; border-radius: 5px; } #drop_zone.hovering { -webkit-box-shadow: inset 0px 0px 50px #BBB; -moz-box-shadow: inset 0px 0px 50px #BBB; -o-box-shadow: inset 0px 0px 50px #BBB; box-shadow: inset 0px 0px 50px #BBB; } #file-upload-box { margin: 40px 25px; padding: 10px; border: 1px solid #BBB; } #description:first-line { margin-left: 42px; } #output_area { text-align: center; display: flex; flex-wrap: wrap; align-content: space-between; position: absolute; left: 0; right: 0; top: 106px; overflow: auto; bottom: 0; } #file-upload-box { position: absolute; top: 45px; bottom: 45px; left: 20px; right: 20px; background-color: #fff; overflow: auto; } .upload-img-itme { color: #333; width: 170px; margin: 10px; color: #333; flex: 1; } .upload-img-itme a.rimg-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 5px; display: block; } .d-image { box-shadow: 0 0 10px #bbb; border-radius: 20px; overflow: hidden; width: 170px; height: 170px; display: inline-block; z-index: 344; background-color: #cecece; position: relative; transition: all 1s; -moz-transition: all 1s; -webkit-transition: all 1s; -o-transition: all 1s; cursor: pointer; } .d-image:hover:after { display: block; } .d-image:after { content: "×"; font-size: 44px; text-align: center; width: 50%; margin: auto; position: absolute; top: 50%; display: none; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } .d-image:hover> img { -webkit-filter: blur(5px); -moz-filter: blur(5px); -ms-filter: blur(5px); filter: blur(5px); } </style> </head>
<body ondrop="return false" ondragover="return false"> <!--防止拖拽跳转--> <div id="file-upload"> <div id="file-upload-box"> <label id="drop_zone">将文件拖拽到这里或点击这里 <input multiple id="files" type="file" hidden="hidden" style="display: none;" name="files[]" /> </label> <div id="output_area"></div> </div> </div> <div style="position: absolute; bottom: 10px; left: 40px; right: 40px;text-align: center;"> <button onclick="demo.ImageLs=[];reloadDiv();" style="padding: 5px 10px; background: #fff; border: 1px #000 solid; cursor: pointer;">清空</button> <button onclick="alert('上传')" style="padding: 5px 10px; background: #fff; border: 1px #000 solid; cursor: pointer;">上传</button> </div> <script> var ImgType = ["gif", "jpeg", "jpg", "bmp", "png", "ico", "webp"]; function getFileUrl(fileObj) { return window.URL.createObjectURL(fileObj); } //拖拽功能 var output = document.getElementById('output_area'); var dropZone = document.getElementById('drop_zone'); if(!(('draggable' in dropZone) && ('ondragenter' in dropZone) && ('ondragleave' in dropZone) && ('ondragover' in dropZone) && window.File)) { document.getElementById('error_msg').style.display = 'block'; document.getElementById('demo_area').style.display = 'none'; } else { function handleFileDragEnter(e) { e.stopPropagation(); e.preventDefault(); this.classList.add('hovering'); } function handleFileDragLeave(e) { e.stopPropagation(); e.preventDefault(); this.classList.remove('hovering'); } function handleFileDragOver(e) { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; } function handleFileDrop(e) { e.stopPropagation(); e.preventDefault(); this.classList.remove('hovering'); FilesGetImgSv(e.dataTransfer.files); } dropZone.addEventListener('dragenter', handleFileDragEnter, false); dropZone.addEventListener('dragleave', handleFileDragLeave, false); dropZone.addEventListener('dragover', handleFileDragOver, false); dropZone.addEventListener('drop', handleFileDrop, false); } //<input function handleFileSelect(evt) { FilesGetImgSv(evt.target.files); } document.getElementById('files').addEventListener('change', handleFileSelect, false); //图片文件 过滤 显示 function FilesGetImgSv(files) {//获取文件 for(var i = 0, f; f = files[i]; i++) { if(RegExp("\.(" + ImgType.join("|") + ")$", "i").test(f.name.toLowerCase())) { //这里是简单后缀验证,可添加f.type验证格式 f.BolbUrl = getFileUrl(f); demo.ImageLs.push(f); } } reloadDiv(); } function reloadDiv(){//刷新视图 var t=""; demo.ImageLs.forEach(function(v,i){ t=t+`<div class="upload-img-itme"><div class="d-image" onclick="demo.removeThisUpImg(${i})"></div><a class="rimg-name"><strong>${v.name}</strong></a></div>`; }); document.getElementById("output_area").innerHTML=t; } var demo = { ImageLs: [], removeThisUpImg: function(index) { demo.ImageLs.splice(index, 1); reloadDiv(); } }; </script> </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
以上是H5怎样做出图片拖拽上传预览组件的详细内容。更多信息请关注PHP中文网其他相关文章!

HTML5的核心特性包括语义化标签、多媒体支持、离线存储与本地存储、表单增强。1.语义化标签如、等,提升代码可读性和SEO效果。2.和标签简化多媒体嵌入。3.离线存储和本地存储如ApplicationCache和LocalStorage,支持无网络运行和数据存储。4.表单增强引入新输入类型和验证属性,简化处理和验证。

H5提供了多种新特性和功能,极大地增强了前端开发的能力。1.多媒体支持:通过和元素嵌入媒体,无需插件。2.画布(Canvas):使用元素动态渲染2D图形和动画。3.本地存储:通过localStorage和sessionStorage实现数据持久化存储,提升用户体验。

H5和HTML5是不同的概念:HTML5是HTML的一个版本,包含新元素和API;H5是基于HTML5的移动应用开发框架。HTML5通过浏览器解析和渲染代码,H5应用则需要容器运行并通过JavaScript与原生代码交互。

HTML5的关键元素包括、、、、、等,用于构建现代网页。1.定义头部内容,2.用于导航链接,3.表示独立文章内容,4.组织页面内容,5.展示侧边栏内容,6.定义页脚,这些元素增强了网页的结构和功能性。

HTML5和H5没有区别,H5是HTML5的简称。1.HTML5是HTML的第五个版本,增强了网页的多媒体和交互功能。2.H5常用于指代基于HTML5的移动网页或应用,适用于各种移动设备。

HTML5是超文本标记语言的最新版本,由W3C标准化。HTML5引入了新的语义化标签、多媒体支持和表单增强,提升了网页结构、用户体验和SEO效果。HTML5引入了新的语义化标签,如、、、等,使网页结构更清晰,SEO效果更好。HTML5支持多媒体元素和,无需第三方插件,提升了用户体验和加载速度。HTML5增强了表单功能,引入了新的输入类型如、等,提高了用户体验和表单验证效率。

如何写出干净高效的HTML5代码?答案是通过语义化标签、结构化代码、性能优化和避免常见错误。1.使用语义化标签如、等,提升代码可读性和SEO效果。2.保持代码结构化和可读性,使用适当缩进和注释。3.优化性能,通过减少不必要的标签、使用CDN和压缩代码。4.避免常见错误,如标签未闭合,确保代码有效性。

H5通过多媒体支持、离线存储和性能优化提升网页用户体验。1)多媒体支持:H5的和元素简化开发,提升用户体验。2)离线存储:WebStorage和IndexedDB允许离线使用,提升体验。3)性能优化:WebWorkers和元素优化性能,减少带宽消耗。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版
好用的JavaScript开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具