DIV CSS 佈局教程網

仿 iPhone 開關風格:用 jQuery 打造單選與複選按鈕
編輯:關於JavaScript     

iOS 那種滑動式開關的視覺語言影響力很強,很多網頁應用都想把它搬進來。本文以 jQuery 搭配一張精靈圖,做出足以假亂真的 Enable/Disable 切換鈕,原生 radio 與 checkbox 只是被隱藏起來的資料載體。

運行效果如下:

線上演示:https://demo.jb51.net/js/2015/js-iphone-radio-checkbox-button-codes/

完整程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>iPhone 風格的單選框和複選框 jQuery 代碼</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready( function(){
    $(".cb-enable").click(function(){
        var parent = $(this).parents('.switch');
        $('.cb-disable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', true);
    });
    $(".cb-disable").click(function(){
        var parent = $(this).parents('.switch');
        $('.cb-enable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', false);
    });
});
</script>
<style type="text/css">
body { font-family: Arial, Sans-serif; padding: 0 20px; }
.field { width: 100%; float: left; margin: 0 0 20px; }
.field input { margin: 0 0 0 20px; }
h3 span { background: #444; color: #fff; padding: 3px; }
pre { background: #f4f4f4; }
.cb-enable, .cb-disable, .cb-enable span, .cb-disable span {
    background: url(switch.gif) repeat-x; display: block; float: left;
}
.cb-enable span, .cb-disable span {
    line-height: 30px; display: block; background-repeat: no-repeat; font-weight: bold;
}
.cb-enable span { background-position: left -90px; padding: 0 10px; }
.cb-disable span { background-position: right -180px; padding: 0 10px; }
.cb-disable.selected { background-position: 0 -30px; }
.cb-disable.selected span { background-position: right -210px; color: #fff; }
.cb-enable.selected { background-position: 0 -60px; }
.cb-enable.selected span { background-position: left -150px; color: #fff; }
.switch label { cursor: pointer; }
.switch input { display: none; }
</style>
</head>
<body>
<h2>iPhone 風格的單選框和複選框 jQuery 代碼</h2>
<p class="field switch">
<input type="radio" id="radio1" name="field" checked />enable
<input type="radio" id="radio2" name="field" />disable
<label for="radio1" class="cb-enable selected"><span>Enable</span></label>
<label for="radio2" class="cb-disable"><span>Disable</span></label>
</p>
<p class="field switch">
<label class="cb-enable"><span>On</span></label>
<label class="cb-disable selected"><span>Off</span></label>
<input type="checkbox" id="checkbox" class="checkbox" name="field2" /> Checkbox
</p>
</body>
</html>

拆解三個步驟

一、HTML 結構

一組開關由兩個 label 組成,各自帶 cb-enable 或 cb-disable 類別,selected 決定目前狀態。單選版本用 label for 綁定 radio;複選版本則把 checkbox 藏在後方,由腳本同步 checked。

二、JavaScript 邏輯

點擊時先 parents('.switch') 找到所屬容器,把另一顆的 selected 拿掉、自己加上,最後更新背後表單元素的 checked 狀態。因為操作範圍限定在容器內,同一頁放多組開關也不會互相干擾。

三、CSS 精靈圖定位

所有外觀都靠 switch.gif 的 background-position 切出來:未選與已選各佔一個垂直區段,左右兩側文字再各自偏移。這也是為什麼 span 要分成左右兩塊——中間的滑塊會遮住其中一側。

相容性

理論上主流瀏覽器都能跑,實測通過的環境為 Firefox 3.5+、IE7+、Chrome 4.1+、Opera 9.6+、Safari 4+。需要留意的是這裡用的是 attr('checked', true),在 jQuery 1.6 之後建議改用 prop(),否則狀態同步會出現非預期的行為。

以上就是這份仿 iPhone 風格單選與複選按鈕的完整實作,希望對你的 JavaScript 專案有幫助。

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