這次帶給大家H5怎樣做出圖片拖曳上傳預覽組件,H5做出圖片拖曳上傳預覽組件的注意事項有哪些,下面就是實戰案例,一起來看一下。
H5中拖曳事件有:
[ - ] DragDrop:拖曳完成,也就是滑鼠拖曳到物件並在拖曳區域釋放。
[ - ] DragEnter :拖放進入,也就是滑鼠拖放物件進入拖放區域。
[ - ] DragLeave:離開拖放區域。
[ - ] DragOver :拖放物件懸浮於拖放區域,在拖放區域內移動時多次觸發。
1.拖曳檔案取得檔案資訊
範例
nbsp;html> <meta> <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> <div> <div>将文件拖放到此处</div> <output></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), ' (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate.toLocaleDateString(), ''); } document.getElementById('list').innerHTML = '<ul>' + output.join('') + ''; } 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>
2.限制檔案大小與檔案格式
nbsp;html> <meta> <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> <div> <div>将文件拖放到此处</div> <output></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), ' (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate.toLocaleDateString(), ''); } } document.getElementById('list').innerHTML = '<ul>' + output.join('') + ''; } 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>
3.顯示縮圖與動態增刪效果
範例
nbsp;html> <meta> <title>简易上传预览</title> <style> #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>
<!--防止拖拽跳转--> <div> <div> <label>将文件拖拽到这里或点击这里 <input> </label> <div></div> </div> </div> <div> <button>清空</button> <button>上传</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})"><a class="rimg-name"><strong>${v.name}`; }); document.getElementById("output_area").innerHTML=t; } var demo = { ImageLs: [], removeThisUpImg: function(index) { demo.ImageLs.splice(index, 1); reloadDiv(); } }; </script>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
以上是H5怎麼做出圖片拖曳上傳預覽組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

HTML5帶來了五個關鍵改進:1.語義化標籤提升了代碼清晰度和SEO效果;2.多媒體支持簡化了視頻和音頻嵌入;3.表單增強簡化了驗證;4.離線與本地存儲提高了用戶體驗;5.畫布與圖形功能增強了網頁的可視化效果。

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.避免常見錯誤,如標籤未閉合,確保代碼有效性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。