js實(shí)現(xiàn)文件上傳表單域美化特效
一款效果非常時(shí)尚的文件上傳表單域美化特效,下面給出制作的簡(jiǎn)要教程。
先上幾個(gè)效果飽飽眼福:
使用方法
這些文件上傳域的美化使用的方法都是隱藏原生的<input type="file">元素,然后使用一個(gè)<label>元素來(lái)制作美化效果。
HTML結(jié)構(gòu)
該文件上傳域美化效果最基本的HTML結(jié)構(gòu)如下:
<input type="file" name="file" id="file" class="inputfile" /> <label for="file">Choose a file</label>
CSS樣式
首先需要隱藏<input>元素。這里不能使用display: none或visibility: hidden來(lái)隱藏它,因?yàn)檫@樣做只后,<input>元素里的值不會(huì)被上傳到服務(wù)器端,而且按TAB鍵時(shí)這個(gè)<input>元素也不會(huì)被找到。隱藏的方法如下:
.inputfile { width: 0.1px; height: 0.1px; opacity: 0; overflow: hidden; position: absolute; z-index: -1; }
接下來(lái)給<label>元素設(shè)置樣式。這里要將<label>元素制作為一個(gè)按鈕的樣式。
.inputfile + label { font-size: 1.25em; font-weight: 700; color: white; background-color: black; display: inline-block; } .inputfile:focus + label, .inputfile + label:hover { background-color: red; }
當(dāng)鼠標(biāo)滑過(guò)label時(shí)需要將光標(biāo)顯示為一個(gè)小手的形狀。
.inputfile + label { cursor: pointer; /* "hand" cursor */ }
為了制作可以使用鍵盤導(dǎo)航的效果,需要添加下面的代碼。
.inputfile:focus + label { outline: 1px dotted #000; outline: -webkit-focus-ring-color auto 5px; }
-webkit-focus-ring-color auto 5px可以在 Chrome,Opera 和 Safari瀏覽器中獲取默認(rèn)的邊框外觀。
如果你使用了類似FastClick(一個(gè)在移動(dòng)觸摸設(shè)備上消除300毫秒tap-pause的工具庫(kù)),并且你需要添加一些文本標(biāo)簽,那么按鈕將不會(huì)正常工作,除非設(shè)置了pointer-events: none。
<label for="file"><strong>Choose a file</strong></label> .inputfile + label * { pointer-events: none; }
JavaScript
最后需要做的事情是標(biāo)識(shí)用戶選擇了哪些文件。原生的文件上傳域是有這個(gè)功能的,但是這里使用的是虛擬的按鈕。特效中使用javascript來(lái)實(shí)現(xiàn)這個(gè)功能。
<input type="file" name="file" id="file" class="inputfile" data-multiple-caption="{count} files selected" multiple /> var inputs = document.querySelectorAll( '.inputfile' ); Array.prototype.forEach.call( inputs, function( input ) { var label = input.nextElementSibling, labelVal = label.innerHTML; input.addEventListener( 'change', function( e ) { var fileName = ''; if( this.files && this.files.length > 1 ) fileName = ( this.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', this.files.length ); else fileName = e.target.value.split( '\\' ).pop(); if( fileName ) label.querySelector( 'span' ).innerHTML = fileName; else label.innerHTML = labelVal; }); });
瀏覽器禁用JavaScript的處理
如果瀏覽器禁用了JavaScript,那么只有使用原生的文件上傳域組件。我們需要做的事情是在<html>元素上添加一個(gè).no-js的class,然后使用Javascript來(lái)替換它。
<html class="no-js"> <head> <!-- remove this if you use Modernizr --> <script>(function(e,t,n){var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0);</script> </head> </html>.
js .inputfile { width: 0.1px; height: 0.1px; opacity: 0; overflow: hidden; position: absolute; z-index: -1; } .no-js .inputfile + label { display: none; }
以上就是js實(shí)現(xiàn)文件上傳表單域美化特效,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
JavaScript使用shift方法移除素組第一個(gè)元素實(shí)例分析
這篇文章主要介紹了JavaScript使用shift方法移除素組第一個(gè)元素的用法,實(shí)例分析了javascript中shift方法的使用技巧,需要的朋友可以參考下2015-04-04javascript中如何將字符串轉(zhuǎn)換成數(shù)字
這篇文章主要介紹了javascript中如何將字符串轉(zhuǎn)換成數(shù)字問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02js匿名函數(shù)的調(diào)用示例(形式多種多樣)
匿名函數(shù)就是沒(méi)有實(shí)際名字的函數(shù),javaScript的匿名函數(shù)形式多樣,下面就一一為大家羅列出來(lái)2014-08-08javascript實(shí)現(xiàn)蒙版與禁止頁(yè)面滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)蒙版與禁止頁(yè)面滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01Jsonp 關(guān)鍵字詳解及json和jsonp的區(qū)別,ajax和jsonp的區(qū)別
這篇文章主要介紹了Jsonp 關(guān)鍵字詳解及json和jsonp的區(qū)別,ajax和jsonp的區(qū)別 的相關(guān)資料,需要的朋友可以參考下2015-12-12