DIV CSS 佈局教程網

用透明度偷換概念:自訂 file 上傳按鈕的外觀
編輯:關於JavaScript     

基於安全性理由,<input type="file"> 的「瀏覽」按鈕外觀一直不聽話。在只能用表單 POST 上傳的舊瀏覽器環境裡, designer 的設計稿往往跟它格格不入。解法很巧妙:把真正的 file 控制項設為透明,蓋在自己畫的按鈕上。

視覺結構

兩個元素疊在一起:一個是自製的文字層(顯示「上傳」字樣),一個是透明但可互動的 file 控制項。旁邊再放一個文字框顯示選到的檔名。

<style>
#ui-upload-holder {
    position: relative; width: 60px; height: 35px;
    border: 1px solid silver; overflow: hidden;
}
/* 真正被點擊的 file 控制項:鋪滿容器且完全透明 */
#ui-upload-input {
    position: absolute; top: 0; right: 0; height: 100%;
    cursor: pointer;
    opacity: 0; filter: alpha(opacity: 0);
    z-index: 999;
}
/* 顯示用的文字層,墊在透明控制項下方 */
#ui-upload-txt {
    position: absolute; top: 0; left: 0;
    width: 100%; height: 100%;
    line-height: 35px; text-align: center;
}
#ui-upload-filepath {
    position: relative; border: 1px solid silver;
    width: 150px; height: 35px; overflow: hidden; float: left;
}
#ui-upload-filepathtxt {
    position: absolute; top: 0; left: 0;
    width: 100%; height: 35px; border: 0; line-height: 35px;
}
</style>

完整頁面

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" >
<title>upload...</title>
<style>
#ui-upload-holder { position:relative; width:60px; height:35px;
                    border:1px solid silver; overflow:hidden; }
#ui-upload-input  { position:absolute; top:0; right:0; height:100%;
                    cursor:pointer; opacity:0; filter:alpha(opacity:0); z-index:999; }
#ui-upload-txt    { position:absolute; top:0; left:0; width:100%; height:100%;
                    line-height:35px; text-align:center; }
#ui-upload-filepath    { position:relative; border:1px solid silver;
                         width:150px; height:35px; overflow:hidden; float:left; }
#ui-upload-filepathtxt { position:absolute; top:0; left:0; width:100%;
                         height:35px; border:0; line-height:35px; }
.uploadlay { margin:200px; border:1px green solid;
             width:300px; height:200px; padding:10px; }
</style>
</head>
<body>
<div class="uploadlay">
  <div id="ui-upload-filepath">
    <input type="text" id="ui-upload-filepathtxt" class="filepathtxt" />
  </div>
  <div id="ui-upload-holder">
    <div id="ui-upload-txt">上傳</div>
    <input type="file" id="ui-upload-input" name="myfile" />
  </div>
</div>

<script>
document.getElementById("ui-upload-input").onchange = function () {
    document.getElementById("ui-upload-filepathtxt").value = this.value;
};
</script>
</body>
</html>

五個實作要點

  1. 為什麼要這麼繞。 在不支援 File API、也不支援 XHR Level 2 的舊瀏覽器(IE9 及以下)裡,上傳只能走表單 POST,而 file 控制項的瀏覽按鈕基於安全考量無法任意改樣式——能改的只有整體透明度。這個技巧就是繞過那個限制。
  2. 寬度別超過 60px。 瀏覽按鈕的寬度無法修改,Firefox 下約 60px。超出就會露出透明層底下不相稱的區塊;而且 Firefox 的 file 控制項只在按鈕區域顯示手型游標,若在容器上設 cursor:pointer,左右兩側的游標會不一致。
  3. 文字層行高等於容器高度。 這樣文字才能垂直居中,視覺上和真正的按鈕對齊。
  4. file 控制項必須真的被點擊。 基於安全,瀏覽器只允許使用者親手點擊「瀏覽」按鈕來開啟檔案選擇器;用 JS 呼叫 input.click() 雖然能彈出視窗,但選完的檔案無法被上傳。所以透明控制項必須覆在文字層上方,而不是反過來。
  5. XMLHttpRequest Level 1 無法上傳檔案。 要做非同步上傳,得用 iframe 搭配表單 POST:把上傳功能整個放進 iframe,或只把處理上傳的伺服器腳本在 iframe 中開啟(將 form 的 target 指向 iframe 名稱)。

今天的做法

這套技巧在 XHR Level 2 與 File API 普及後已無必要。現代瀏覽器都能用 FormData 直接非同步上傳,並在傳送前取得檔案名稱、大小與型別:

<label class="btn">
  上傳
  <input type="file" id="picker" hidden>
</label>

<script>
document.getElementById('picker').addEventListener('change', function () {
    var file = this.files[0];
    if (!file) return;

    var fd = new FormData();
    fd.append('myfile', file);

    fetch('/upload', { method: 'POST', body: fd })
        .then(function (r) { return r.json(); })
        .then(function (data) { console.log('上傳完成', data); });
});
</script>

用 <label> 包住 hidden 的 file 輸入,點標籤就會開啟檔案選擇器,而且鍵盤操作與螢幕閱讀器都能正常運作——這比透明疊層更簡單,也更友善。若堅持要自訂外觀,記得幫自製按鈕加上 role="button" 與鍵盤回饋,別讓它變成只有滑鼠按得動的裝飾品。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved