使用css及js自定义文件上传按钮
10月12日, 2015 CSS JavaScript Tumars 3,708 views次
10月12日, 2015 3,708 views次
目录
前言
本文主要是利用 <label>
跟js做一些按钮样式功能跟样式上的自定义。并考虑了兼容性、js禁用、浏览器bug。主要思路如下:
- 隐藏
<input>
- 给
<label>
定义样式充当上传按钮 - 用 js 获得选定的文件名与数量
我们先来看一个单纯的 input :
1 2 |
<ol class="linenums"><li class="L0"><code><span class="tag"><input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"inputfile"</span><span class="pln"> </span><span class="tag">/></span></code></li><li class="L1"><code><span class="tag"><label</span><span class="pln"> </span><span class="atn">for</span><span class="pun">=</span><span class="atv">"file"</span><span class="tag">></span><span class="pln">Choose a file</span><span class="tag"></label></span></code></li></ol> |
接下来开始一步步优化这个文件选择按钮。
隐藏 Input
首先我们先把难看的输入框隐藏起来。注意这时是不能用 display: none
或 visibility: hidden
的。理由是:这样做的话input 的值就无法通过 submit 发送到服务器,input 按钮也无法被 tab 键被选中。我们可以使用一些 css 属性隐藏 input 同时让它对浏览器是可见的:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0.1px</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0.1px</span><span class="pun">;</span></code></li><li class="L3"><code><span class="pln"> opacity</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pln"> overflow</span><span class="pun">:</span><span class="pln"> hidden</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln"> position</span><span class="pun">:</span><span class="pln"> absolute</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln"> z</span><span class="pun">-</span><span class="pln">index</span><span class="pun">:</span><span class="pln"> </span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span></code></li><li class="L7"><code><span class="pun">}</span></code></li></ol> |
这里解释下为什么把 width
和 height
设置为 0.1px
而不是 0px
,把宽高设成0的话在一些浏览器中会被排除在 tab 键选择之外。另外 position: absolute;
可以保证它不会影响周围元素。
给 Label 定义样式
主要就是让 <label>
看起来像个按钮,简单的写一下:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">+</span><span class="pln"> label </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> font</span><span class="pun">-</span><span class="pln">size</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1.25em</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln"> font</span><span class="pun">-</span><span class="pln">weight</span><span class="pun">:</span><span class="pln"> </span><span class="lit">700</span><span class="pun">;</span></code></li><li class="L3"><code><span class="pln"> color</span><span class="pun">:</span><span class="pln"> white</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pln"> background</span><span class="pun">-</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> black</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln"> display</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">inline</span><span class="pun">-</span><span class="pln">block</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pun">}</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pun">.</span><span class="pln">inputfile</span><span class="pun">:</span><span class="pln">focus </span><span class="pun">+</span><span class="pln"> label</span><span class="pun">,</span></code></li><li class="L9"><code><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">+</span><span class="pln"> label</span><span class="pun">:</span><span class="pln">hover </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln"> background</span><span class="pun">-</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> red</span><span class="pun">;</span></code></li><li class="L1"><code><span class="pun">}</span></code></li></ol> |
网页无障碍 Web Accessibility
为了让效果更友好,我们需要让按钮被选中时产生变化,以便让用户知道按钮被选中了。
我们先让鼠标覆盖到按钮上时变成小手:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">+</span><span class="pln"> label </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> cursor</span><span class="pun">:</span><span class="pln"> pointer</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* "hand" cursor */</span></code></li><li class="L2"><code><span class="pun">}</span></code></li></ol> |
键盘导航
某些用户只能使用键盘来操作页面,我们得让按钮被聚焦到时产生点变化表示被选中,以便这些用户操作。可以使用css选择器:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">inputfile</span><span class="pun">:</span><span class="pln">focus </span><span class="pun">+</span><span class="pln"> label </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> outline</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> dotted </span><span class="com">#000;</span></code></li><li class="L2"><code><span class="pln"> outline</span><span class="pun">:</span><span class="pln"> </span><span class="pun">-</span><span class="pln">webkit</span><span class="pun">-</span><span class="pln">focus</span><span class="pun">-</span><span class="pln">ring</span><span class="pun">-</span><span class="pln">color </span><span class="kwd">auto</span><span class="pln"> </span><span class="lit">5px</span><span class="pun">;</span></code></li><li class="L3"><code><span class="pun">}</span></code></li></ol> |
提示:-webkit-focus-ring-color auto 5px
可以在 Chrome, Opera 和 Safari 显示的像一个默认的外框线。在非 webkit 浏览器下则使用第一条 outline 规则。
可能出现的点击问题
如果你使用FastClick(一个用来处理移动端 click
事件300毫秒延迟的库)并且打算在 lable
里加一些标签,按钮可能就无法工作了。可以使用 pointer-events: none
来解决这个问题:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="tag"><label</span><span class="pln"> </span><span class="atn">for</span><span class="pun">=</span><span class="atv">"file"</span><span class="tag">><strong></span><span class="pln">Choose a file</span><span class="tag"></strong></label></span></code></li></ol> |
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">+</span><span class="pln"> label </span><span class="pun">*</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> pointer</span><span class="pun">-</span><span class="pln">events</span><span class="pun">:</span><span class="pln"> none</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pun">}</span></code></li></ol> |
提示:pointer-events: none
在解决点击穿透问题时还是很好用的,具体的相关介绍可以查看张鑫旭大神的这篇文章:CSS3 pointer-events:none应用举例及扩展
通过 JavaScript 改善
我们还需要能够显示选择了多少文件,input
是能够做到这个的,但是我们已经把它给隐藏了。我们可以使用一点点 js 让 lable
的内容变成选择的文件名或数量。
1 2 |
<ol class="linenums"><li class="L0"><code><span class="tag"><input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"inputfile"</span><span class="pln"> </span><span class="atn">data-multiple-caption</span><span class="pun">=</span><span class="atv">"{count} files selected"</span><span class="pln"> </span><span class="atn">multiple</span><span class="pln"> </span><span class="tag">/></span></code></li></ol> |
1 2 |
<ol class="linenums"><li class="L0"><code><span class="kwd">var</span><span class="pln"> inputs </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">querySelectorAll</span><span class="pun">(</span><span class="pln"> </span><span class="str">'.inputfile'</span><span class="pln"> </span><span class="pun">);</span></code></li><li class="L1"><code><span class="typ">Array</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">forEach</span><span class="pun">.</span><span class="pln">call</span><span class="pun">(</span><span class="pln"> inputs</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln"> input </span><span class="pun">)</span></code></li><li class="L2"><code><span class="pun">{</span></code></li><li class="L3"><code><span class="pln"> </span><span class="kwd">var</span><span class="pln"> label </span><span class="pun">=</span><span class="pln"> input</span><span class="pun">.</span><span class="pln">nextElementSibling</span><span class="pun">,</span></code></li><li class="L4"><code><span class="pln"> labelVal </span><span class="pun">=</span><span class="pln"> label</span><span class="pun">.</span><span class="pln">innerHTML</span><span class="pun">;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln"> input</span><span class="pun">.</span><span class="pln">addEventListener</span><span class="pun">(</span><span class="pln"> </span><span class="str">'change'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln"> e </span><span class="pun">)</span></code></li><li class="L7"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln"> </span><span class="kwd">var</span><span class="pln"> fileName </span><span class="pun">=</span><span class="pln"> </span><span class="str">''</span><span class="pun">;</span></code></li><li class="L9"><code><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">files </span><span class="pun">&&</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">files</span><span class="pun">.</span><span class="pln">length </span><span class="pun">></span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">)</span></code></li><li class="L0"><code><span class="pln"> fileName </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">getAttribute</span><span class="pun">(</span><span class="pln"> </span><span class="str">'data-multiple-caption'</span><span class="pln"> </span><span class="pun">)</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> </span><span class="str">''</span><span class="pln"> </span><span class="pun">).</span><span class="pln">replace</span><span class="pun">(</span><span class="pln"> </span><span class="str">'{count}'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">files</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span></code></li><li class="L1"><code><span class="pln"> </span><span class="kwd">else</span></code></li><li class="L2"><code><span class="pln"> fileName </span><span class="pun">=</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">split</span><span class="pun">(</span><span class="pln"> </span><span class="str">'\\'</span><span class="pln"> </span><span class="pun">).</span><span class="pln">pop</span><span class="pun">();</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln"> fileName </span><span class="pun">)</span></code></li><li class="L5"><code><span class="pln"> label</span><span class="pun">.</span><span class="pln">querySelector</span><span class="pun">(</span><span class="pln"> </span><span class="str">'span'</span><span class="pln"> </span><span class="pun">).</span><span class="pln">innerHTML </span><span class="pun">=</span><span class="pln"> fileName</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln"> </span><span class="kwd">else</span></code></li><li class="L7"><code><span class="pln"> label</span><span class="pun">.</span><span class="pln">innerHTML </span><span class="pun">=</span><span class="pln"> labelVal</span><span class="pun">;</span></code></li><li class="L8"><code><span class="pln"> </span><span class="pun">});</span></code></li><li class="L9"><code><span class="pun">});</span></code></li></ol> |
再来个jQuery版的吧,现在也是很少有人用原生js了:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pln">$</span><span class="pun">(</span><span class="pln"> </span><span class="str">'.inputfile'</span><span class="pln"> </span><span class="pun">).</span><span class="pln">each</span><span class="pun">(</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span></code></li><li class="L1"><code><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> </span><span class="kwd">var</span><span class="pln"> $input </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="pln"> </span><span class="kwd">this</span><span class="pln"> </span><span class="pun">),</span></code></li><li class="L3"><code><span class="pln"> $label </span><span class="pun">=</span><span class="pln"> $input</span><span class="pun">.</span><span class="kwd">next</span><span class="pun">(</span><span class="pln"> </span><span class="str">'label'</span><span class="pln"> </span><span class="pun">),</span></code></li><li class="L4"><code><span class="pln"> labelVal </span><span class="pun">=</span><span class="pln"> $label</span><span class="pun">.</span><span class="pln">html</span><span class="pun">();</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln"> $input</span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="pln"> </span><span class="str">'change'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln"> e </span><span class="pun">)</span></code></li><li class="L7"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln"> </span><span class="kwd">var</span><span class="pln"> fileName </span><span class="pun">=</span><span class="pln"> </span><span class="str">''</span><span class="pun">;</span></code></li><li class="L9"><code></code></li><li class="L0"><code><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">files </span><span class="pun">&&</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">files</span><span class="pun">.</span><span class="pln">length </span><span class="pun">></span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">)</span></code></li><li class="L1"><code><span class="pln"> fileName </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">getAttribute</span><span class="pun">(</span><span class="pln"> </span><span class="str">'data-multiple-caption'</span><span class="pln"> </span><span class="pun">)</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> </span><span class="str">''</span><span class="pln"> </span><span class="pun">).</span><span class="pln">replace</span><span class="pun">(</span><span class="pln"> </span><span class="str">'{count}'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">files</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span></code></li><li class="L2"><code><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">value </span><span class="pun">)</span></code></li><li class="L3"><code><span class="pln"> fileName </span><span class="pun">=</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">split</span><span class="pun">(</span><span class="pln"> </span><span class="str">'\\'</span><span class="pln"> </span><span class="pun">).</span><span class="pln">pop</span><span class="pun">();</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln"> fileName </span><span class="pun">)</span></code></li><li class="L6"><code><span class="pln"> $label</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="pln"> </span><span class="str">'span'</span><span class="pln"> </span><span class="pun">).</span><span class="pln">html</span><span class="pun">(</span><span class="pln"> fileName </span><span class="pun">);</span></code></li><li class="L7"><code><span class="pln"> </span><span class="kwd">else</span></code></li><li class="L8"><code><span class="pln"> $label</span><span class="pun">.</span><span class="pln">html</span><span class="pun">(</span><span class="pln"> labelVal </span><span class="pun">);</span></code></li><li class="L9"><code><span class="pln"> </span><span class="pun">});</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln"> </span><span class="com">// Firefox bug fix</span></code></li><li class="L2"><code><span class="pln"> $input</span></code></li><li class="L3"><code><span class="pln"> </span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="pln"> </span><span class="str">'focus'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"> $input</span><span class="pun">.</span><span class="pln">addClass</span><span class="pun">(</span><span class="pln"> </span><span class="str">'has-focus'</span><span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="pun">})</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="pln"> </span><span class="str">'blur'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"> $input</span><span class="pun">.</span><span class="pln">removeClass</span><span class="pun">(</span><span class="pln"> </span><span class="str">'has-focus'</span><span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="pun">});</span></code></li><li class="L5"><code><span class="pun">});</span></code></li></ol> |
解释下 js 的几个点:
- 添加
[multiple]
属性可以允许用户上传多个文件。 - HTML 的
[multiple]
跟 JS 的 file 属性都是只支持现代浏览器,ie6、7、8都是不支持的。对于后一种情况,可以简单的使用 input 的值来改善。例如获得 input 的值是C:\fakepath\filename.jpg
,可以使用split( '\\' ).pop()
来提取文件名。 - 在一些浏览器里可以使用 ESC 按键取消选择,这样input的值就会被重置。所以我们先定义了个变量
labelVal
把lable
的默认值保存起来,以便使用。
如果 JavaScript 被禁用了怎么办?
我们在 css 里把 input
给隐藏掉了。所以在 js 被禁用时要更改下css样式。先给页面添加个 no-js
类名,如果 js 可用,就替换为 js
类名。以此来控制 input
的样式:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="tag"><html</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"no-js"</span><span class="tag">></span></code></li><li class="L1"><code><span class="pln"> </span><span class="tag"><head></span></code></li><li class="L2"><code><span class="pln"> </span><span class="com"><!-- remove this if you use Modernizr --></span></code></li><li class="L3"><code><span class="pln"> </span><span class="tag"><script></span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">,</span><span class="pln">t</span><span class="pun">,</span><span class="pln">n</span><span class="pun">){</span><span class="kwd">var</span><span class="pln"> r</span><span class="pun">=</span><span class="pln">e</span><span class="pun">.</span><span class="pln">querySelectorAll</span><span class="pun">(</span><span class="str">"html"</span><span class="pun">)[</span><span class="lit">0</span><span class="pun">];</span><span class="pln">r</span><span class="pun">.</span><span class="pln">className</span><span class="pun">=</span><span class="pln">r</span><span class="pun">.</span><span class="pln">className</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/(^|\s)no-js(\s|$)/</span><span class="pun">,</span><span class="str">"$1js$2"</span><span class="pun">)})(</span><span class="pln">document</span><span class="pun">,</span><span class="pln">window</span><span class="pun">,</span><span class="lit">0</span><span class="pun">);</span><span class="tag"></script></span></code></li><li class="L4"><code><span class="pln"> </span><span class="tag"></head></span></code></li><li class="L5"><code><span class="tag"></html></span></code></li></ol> |
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">js </span><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0.1px</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0.1px</span><span class="pun">;</span></code></li><li class="L3"><code><span class="pln"> opacity</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pln"> overflow</span><span class="pun">:</span><span class="pln"> hidden</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln"> position</span><span class="pun">:</span><span class="pln"> absolute</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln"> z</span><span class="pun">-</span><span class="pln">index</span><span class="pun">:</span><span class="pln"> </span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span></code></li><li class="L7"><code><span class="pun">}</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pun">.</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">js </span><span class="pun">.</span><span class="pln">inputfile </span><span class="pun">+</span><span class="pln"> label </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln"> display</span><span class="pun">:</span><span class="pln"> none</span><span class="pun">;</span></code></li><li class="L1"><code><span class="pun">}</span></code></li></ol> |
Firefox 浏览器的 bug
很出乎意料的是火狐浏览器会完全的忽略 input[type="file"]:focus
选择器,但是 :hover
和 :active
却能正常选择。不过火狐浏览器允许在js里获取 focus
事件,所以在火狐浏览器里可以给 input 元素添加类名控制 focus 样式:
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pln">input</span><span class="pun">.</span><span class="pln">addEventListener</span><span class="pun">(</span><span class="pln"> </span><span class="str">'focus'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"> input</span><span class="pun">.</span><span class="pln">classList</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln"> </span><span class="str">'has-focus'</span><span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="pun">});</span></code></li><li class="L1"><code><span class="pln">input</span><span class="pun">.</span><span class="pln">addEventListener</span><span class="pun">(</span><span class="pln"> </span><span class="str">'blur'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"> input</span><span class="pun">.</span><span class="pln">classList</span><span class="pun">.</span><span class="pln">remove</span><span class="pun">(</span><span class="pln"> </span><span class="str">'has-focus'</span><span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="pun">});</span></code></li></ol> |
1 2 |
<ol class="linenums"><li class="L0"><code><span class="pun">.</span><span class="pln">inputfile</span><span class="pun">:</span><span class="pln">focus </span><span class="pun">+</span><span class="pln"> label</span><span class="pun">,</span></code></li><li class="L1"><code><span class="pun">.</span><span class="pln">inputfile</span><span class="pun">.</span><span class="pln">has</span><span class="pun">-</span><span class="pln">focus </span><span class="pun">+</span><span class="pln"> label </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> outline</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> dotted </span><span class="com">#000;</span></code></li><li class="L3"><code><span class="pln"> outline</span><span class="pun">:</span><span class="pln"> </span><span class="pun">-</span><span class="pln">webkit</span><span class="pun">-</span><span class="pln">focus</span><span class="pun">-</span><span class="pln">ring</span><span class="pun">-</span><span class="pln">color </span><span class="kwd">auto</span><span class="pln"> </span><span class="lit">5px</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pun">}</span></code></li></ol> |
翻译完了,这里是原文地址,有兴趣的可以去看下。
文中的方法也可以用在单选框、复选框样式自定义上。关于兼容性、js禁用下、火狐浏览器bug都是平时没注意到的地方,以后可以注意下。