js實(shí)現(xiàn)iPhone界面風(fēng)格的單選框和復(fù)選框按鈕實(shí)例
本文實(shí)例講述了js實(shí)現(xiàn)iPhone界面風(fēng)格的單選框和復(fù)選框按鈕。分享給大家供大家參考。具體如下:
這里使用JS美化仿iPhone風(fēng)格的單選框和復(fù)選框按鈕效果,使用了jQuery代碼,附有完整實(shí)例及使用方法,現(xiàn)在,iPhone風(fēng)格確實(shí)流行,希望大家也喜歡。
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/js/2015/js-iphone-radio-checkbox-button-codes/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>iPhone風(fēng)格的單選框和復(fù)選框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; }
</style>
</head>
<body>
<h2>iPhone風(fēng)格的單選框和復(fù)選框jQuery代碼</h2>
<h4>From DevGrow, a blog about designing, developing and growing your website.</h4>
<h3>The Example:</h3>
<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>
<h3>The Prerequisites</h3>
<p>You need just two things for this to work correctly: JQuery 1.3.2+ and the images/switch.gif image file used for the backgrounds.</p>
<h3><span>Step 1</span> The HTML</h3>
<pre><code>
<p class="field switch">
<input type="radio" id="radio1" name="field" checked />
<input type="radio" id="radio2" name="field" />
<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" />
</p>
</code>
</pre>
<h3><span>Step 2</span> The Javascript</h3>
<pre><code>
$(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);
});
});</code>
</pre>
<h3><span>Step 3</span> The CSS</h3>
<pre><code>
.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; }</code>
</pre>
<h3>Compatability</h3>
<p>While this should work in all major browsers, it has only been tested on: Firefox 3.5+, IE7+, Chrome 4.1+, Opera 9.6+, Safari 4+</p>
<h3> </h3>
</body>
</html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
tangram.js庫實(shí)現(xiàn)js類的方式實(shí)例分析
這篇文章主要介紹了tangram.js庫實(shí)現(xiàn)js類的方式,結(jié)合實(shí)例形式分析了tangram.js庫實(shí)現(xiàn)類的創(chuàng)建、繼承等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
ES6擴(kuò)展運(yùn)算符和rest運(yùn)算符用法實(shí)例分析
這篇文章主要介紹了ES6擴(kuò)展運(yùn)算符和rest運(yùn)算符用法,結(jié)合實(shí)例形式分析了ES6擴(kuò)展運(yùn)算符和rest運(yùn)算符基本功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
利用JavaScript編寫一個(gè)簡(jiǎn)單的1024小游戲
在每年的10月24日,我們都會(huì)慶祝程序員節(jié),這是一個(gè)向所有辛勤工作、創(chuàng)造出無數(shù)令人驚嘆應(yīng)用和系統(tǒng)的程序員們致敬的日子,為了紀(jì)念這個(gè)特殊的日子,我們將通過編寫一個(gè)簡(jiǎn)單的1024小游戲來向所有程序員們表示敬意,本文將詳細(xì)解釋如何使用JavaScript編寫這個(gè)小游戲2023-10-10
js驗(yàn)證真實(shí)姓名與身份證號(hào)是否匹配
使用javascript技術(shù)驗(yàn)真實(shí)姓名,要用到unicode字符來匹配,由于中國人的姓名長(zhǎng)度一般是在2-4這個(gè)范圍內(nèi),所以重復(fù)匹配{2,4}次。接下來,通過本文給大家分享js驗(yàn)證省份證號(hào)與真實(shí)姓名是否匹配,需要的朋友可以參考下2015-10-10
分離式j(luò)avascript取當(dāng)前element值的代碼
比較不錯(cuò)的分離式j(luò)s代碼,獲取element的值,大家注意下,運(yùn)行后的效果是32之類的值,其實(shí)主要是沒有強(qiáng)制轉(zhuǎn)換成數(shù)字,所以大家可以加上2008-05-05
學(xué)習(xí)Javascript面向?qū)ο缶幊讨庋b
這篇文章主要幫助大家學(xué)習(xí)Javascript面向?qū)ο缶幊讨庋b,由淺入深的介紹了封裝的概念定義,感興趣的小伙伴們可以參考一下2016-02-02

