
一、原理
1. 運用 label 標籤 幾年前寫「自訂 Input File 檔案上傳按鈕 CSS 最佳解法」有提過「二、- Label 標籤有一個奇特的特性,只要他包在任何 Input 標籤外面,點擊 Label 就等於點擊 Input
- 所以當 Label 標籤很寬、很大時,點擊 Input 就很方便
- 利用這個特性,將 Label 標籤包在 input 上傳按鈕外面,再將 input 上傳按鈕隱藏起來,點擊依然有效
- 原本對 input 上傳按鈕無法設定的任何 CSS,此時設定在 Label 上即可。
- 上方有三個選單分頁,是單選切切換的型態
- 下方則是分頁的內容,點擊該分頁時才顯示對應的分頁內容
二、單選按鈕範例程式碼
以下提供簡單的單選按鈕範例程式碼,實作頁面可參考 "線上看電視" 的「戲劇節目進階篩選」頁面:<div id="radio">
<label><input type="radio" name="label" value="台劇" checked="checked"><span class="round button">台劇</span></label>
<label><input type="radio" name="label" value="古裝劇"><span class="round button">古裝劇</span></label>
<label><input type="radio" name="label" value="陸劇"><span class="round button">陸劇</span></label>
<label><input type="radio" name="label" value="韓劇"><span class="round button">韓劇</span></label>
<label><input type="radio" name="label" value="鄉土時代傳統劇"><span class="round button">鄉土傳統劇</span></label>
<label><input type="radio" name="label" value="大愛劇"><span class="round button">大愛劇</span></label>
</div>
<style>
#radio input[type="radio"] {display: none; }
#radio input:checked + .button {background: #5e7380; color: #fff; cursor: default; }
#radio .button {display: inline-block; margin: 0 5px 10px 0; padding: 5px 10px; background: #f7f7f7; color: #333; cursor: pointer; }
#radio .button:hover {background: #bbb; color: #fff; }
#radio .round {border-radius: 5px; }
</style>
- 所有 name 設定相同字串,才能有單選效果(value 不一定要設定,此處有設定是方便 JS 取值)
- hover 時加上手掌游標,代表可選取,並設定不同的顏色樣式
- 已選取的按鈕使用箭頭游標,代表不可選取
三、多選按鈕範例程式碼
多選按鈕原理一模一樣,前面的 CSS 稍微修改即可,以下提供簡單的範例程式碼,取自 "線上看電視" 的綜藝節目標籤:<div id="checkbox">
<label><input type="checkbox" name="variety" value="綜藝" checked="checked" /><span class="round button">綜藝</span></label>
<label><input type="checkbox" name="variety" value="節目" checked="checked" /><span class="round button">節目</span></label>
<label><input type="checkbox" name="variety" value="隨選隨看" checked="checked" /><span class="round button">隨選隨看</span></label>
<label><input type="checkbox" name="variety" value="直播" /><span class="round button">直播</span></label>
<label><input type="checkbox" name="variety" value="台綜" /><span class="round button">台綜</span></label>
<label><input type="checkbox" name="variety" value="陸綜" /><span class="round button">陸綜</span></label>
<label><input type="checkbox" name="variety" value="韓綜" /><span class="round button">韓綜</span></label>
<label><input type="checkbox" name="variety" value="遊戲" /><span class="round button">遊戲</span></label>
<label><input type="checkbox" name="variety" value="益智" /><span class="round button">益智</span></label>
<label><input type="checkbox" name="variety" value="談話" /><span class="round button">談話</span></label>
<label><input type="checkbox" name="variety" value="娛樂" /><span class="round button">娛樂</span></label>
<label><input type="checkbox" name="variety" value="網紅" /><span class="round button">網紅</span></label>
</div>
<style>
#checkbox input[type="checkbox"] {display: none; }
#checkbox input:checked + .button {background: #5e7380; color: #fff;}
#checkbox .button {display: inline-block; margin: 0 5px 10px 0; padding: 5px 10px; background: #f7f7f7; color: #333; cursor: pointer; }
#checkbox .button:hover {background: #bbb; color: #fff; }
#checkbox .round {border-radius: 5px; }
</style>
下面是以上程式碼的實際效果:
更多 CSS 相關技巧:
沒有留言:
張貼留言注意事項:
◎ 勾選「通知我」可收到後續回覆的mail!
◎ 請在相關文章留言,與文章無關的主題可至「Blogger 社團」提問。
◎ 請避免使用 Safari 瀏覽器,否則無法登入 Google 帳號留言(只能匿名留言)!
◎ 提問若無法提供足夠的資訊供判斷,可能會被無視。建議先參考這篇「Blogger 提問技巧及注意事項」。
◎ CSS 相關問題非免費諮詢,建議使用「Chrome 開發人員工具」尋找答案。
◎ 手機版相關問題請參考「Blogger 行動版範本的特質」→「三、行動版範本不一定能執行網頁版工具」;或參考「Blogger 行動版範本修改技巧 」,或本站 Blogger 行動版標籤相關文章。
◎ 非官方範本問題、或貴站為商業網站,請參考「Blogger 免費諮詢 + 付費諮詢」
◎ 若是使用官方 RWD 範本,請參考「Blogger 推出全新自適應 RWD 官方範本及佈景主題」→ 不建議對範本進行修改!
◎ 若留言要輸入語法,"<"、">"這兩個符號請用其他符號代替,否則語法會消失!
◎ 為了過濾垃圾留言,所有留言不會即時發佈,請稍待片刻。
◎ 本站「已關閉自刪留言功能」。