表单/表格

时尚文件上传按钮表单域美化特效

阿里云


这是一款效果非常酷的文件上传表单域美化特效。该特效有 7 种不同的美化文件上传域的效果。其中有 6 种使用 SVG 来制作文件上传域的外观。这些文件上传域的美化效果时尚大方,非常的漂亮。这些文件上传域的美化使用的方法都是隐藏原生的元素,然后使用一个

HTML 结构

该文件上传域美化效果最基本的 HTML 结构如下:

也想出现在这里?联系我们
创客主机
  1. <input type="file" name="file" id="file" class="inputfile" />
  2. <label for="file">Choose a file</label>

CSS 样式

首先需要隐藏<input>元素。这里不能使用 display: none 或 visibility: hidden 来隐藏它,因为这样做只后,<input>元素里的值不会被上传到服务器端,而且按 TAB 键时这个<input>元素也不会被找到。隐藏的方法如下:

  1. .inputfile {
  2.   width: 0.1px;
  3.   height: 0.1px;
  4.   opacity: 0;
  5.   overflow: hidden;
  6.   position: absolute;
  7.   z-index: -1;
  8. }

接下来给<label>元素设置样式。这里要将<label>元素制作为一个按钮的样式。

  1. .inputfile + label {
  2.     font-size: 1.25em;
  3.     font-weight: 700;
  4.     color: white;
  5.     background-color: black;
  6.     display: inline-block;
  7. }
  8.  
  9. .inputfile:focus + label,
  10. .inputfile + label:hover {
  11.     background-color: red;
  12. }

当鼠标滑过 label 时需要将光标显示为一个小手的形状。

  1. .inputfile + label {
  2.   cursor: pointer; /* "hand" cursor */
  3. }

为了制作可以使用键盘导航的效果,需要添加下面的代码。

  1. .inputfile:focus + label {
  2.   outline: 1px dotted #000;
  3.   outline: -webkit-focus-ring-color auto 5px;
  4. }

-webkit-focus-ring-color auto 5px 可以在 Chrome,Opera 和 Safari 浏览器中获取默认的边框外观。

如果你使用了类似 FastClick(一个在移动触摸设备上消除 300 毫秒 tap-pause 的工具库),并且你需要添加一些文本标签,那么按钮将不会正常工作,除非设置了 pointer-events: none。

  1. <label for="file"><strong>Choose a file</strong></label>
  1. .inputfile + label * {
  2.   pointer-events: none;
  3. }

JavaScript

最后需要做的事情是标识用户选择了哪些文件。原生的文件上传域是有这个功能的,但是这里使用的是虚拟的按钮。特效中使用 javascript 来实现这个功能。

  1. <input type="file" name="file" id="file" class="inputfile" data-multiple-caption="{count} files selected" multiple />
  1. var inputs = document.querySelectorAll( '.inputfile' );
  2. Array.prototype.forEach.call( inputs, function( input )
  3. {
  4.   var label  = input.nextElementSibling,
  5.     labelVal = label.innerHTML;
  6.  
  7.   input.addEventListener( 'change', function( e )
  8.   {
  9.     var fileName = '';
  10.     if( this.files && this.files.length > 1 )
  11.       fileName = ( this.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', this.files.length );
  12.     else
  13.       fileName = e.target.value.split( '\\' ).pop();
  14.  
  15.     if( fileName )
  16.       label.querySelector( 'span' ).innerHTML = fileName;
  17.     else
  18.       label.innerHTML = labelVal;
  19.   });
  20. });

浏览器禁用 JavaScript 的处理

如果浏览器禁用了 JavaScript,那么只有使用原生的文件上传域组件。我们需要做的事情是在元素上添加一个.no-js 的 class,然后使用 Javascript 来替换它。

  1. <html class="no-js">
  2.     <head>
  3.         <!-- remove this if you use Modernizr -->
  4.         <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>
  5.     </head>
  6. </html>
  1. .js .inputfile {
  2.     width: 0.1px;
  3.     height: 0.1px;
  4.     opacity: 0;
  5.     overflow: hidden;
  6.     position: absolute;
  7.     z-index: -1;
  8. }
  9.  
  10. .no-js .inputfile + label {
  11.     display: none;
  12. }

原文地址:http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/

时尚文件上传按钮表单域美化特效

已有 364 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!