js实现iPhone界面风格的单选框和复选框按钮实例


Posted in Javascript onAugust 18, 2015

本文实例讲述了js实现iPhone界面风格的单选框和复选框按钮。分享给大家供大家参考。具体如下:

这里使用JS美化仿iPhone风格的单选框和复选框按钮效果,使用了jQuery代码,附有完整实例及使用方法,现在,iPhone风格确实流行,希望大家也喜欢。

运行效果截图如下:

js实现iPhone界面风格的单选框和复选框按钮实例

在线演示地址如下:

具体代码如下:

<!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风格的单选框和复选框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风格的单选框和复选框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>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
页面载入结束自动调用js函数示例
Sep 23 Javascript
再探JavaScript作用域
Sep 24 Javascript
jQuery+PHP星级评分实现方法
Oct 02 Javascript
jQuery中判断对象是否存在的方法汇总
Feb 24 Javascript
基于Node.js的JavaScript项目构建工具gulp的使用教程
May 20 Javascript
jQuery继承extend用法详解
Oct 10 Javascript
node.js报错:Cannot find module 'ejs'的解决办法
Dec 14 Javascript
AngularJS中update两次出现$promise属性无法识别的解决方法
Jan 05 Javascript
svg动画之动态描边效果
Feb 22 Javascript
JavaScript数据结构之双向链表定义与使用方法示例
Oct 27 Javascript
微信小程序封装多张图片上传api代码实例
Dec 30 Javascript
详解如何使用React Hooks请求数据并渲染
Oct 18 Javascript
JS+DIV+CSS实现仿表单下拉列表效果
Aug 18 #Javascript
js模拟淘宝网的多级选择菜单实现方法
Aug 18 #Javascript
js实现Select头像选择实时预览代码
Aug 17 #Javascript
基于jquery实现放大镜效果
Aug 17 #Javascript
js实现浏览本地文件并显示扩展名的方法
Aug 17 #Javascript
JS模仿编辑器实时改变文本框宽度和高度大小的方法
Aug 17 #Javascript
js实现匹配时换色的输入提示特效代码
Aug 17 #Javascript
You might like
在IIS7.0下面配置PHP 5.3.2运行环境的方法
2010/04/13 PHP
基于PHP array数组的教程详解
2013/06/05 PHP
php判断两个浮点数是否相等的方法
2015/03/14 PHP
Yii清理缓存的方法
2016/01/06 PHP
javascript parseInt() 函数的进制转换注意细节
2013/01/08 Javascript
javascript打印输出json实例
2013/11/11 Javascript
Node.js入门教程:在windows和Linux上安装配置Node.js图文教程
2014/08/14 Javascript
JS实现的4种数字千位符格式化方法分享
2015/03/02 Javascript
Bootstrap优化站点资源、响应式图片、传送带使用详解3
2016/10/14 Javascript
简单谈谈js的数据类型
2017/09/25 Javascript
layui之select的option叠加问题的解决方法
2018/03/08 Javascript
通过说明与示例了解js五种设计模式
2019/06/17 Javascript
layui 实现加载动画以及非真实加载进度的方法
2019/09/23 Javascript
详解Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结
2020/05/28 Javascript
javascript canvas封装动态时钟
2020/09/30 Javascript
[01:45]绝对公平!DOTA2队长征召模式详解
2014/04/25 DOTA
Python用户推荐系统曼哈顿算法实现完整代码
2017/12/01 Python
Python之pandas读写文件乱码的解决方法
2018/04/20 Python
终端命令查看TensorFlow版本号及路径的方法
2018/06/13 Python
python实现生成字符串大小写字母和数字的各种组合
2019/01/01 Python
python多线程实现TCP服务端
2019/09/03 Python
Django admin 实现search_fields精确查询实例
2020/03/30 Python
浅谈Django中的QueryDict元素为数组的坑
2020/03/31 Python
Django分组聚合查询实例分享
2020/04/29 Python
完美解决Django2.0中models下的ForeignKey()问题
2020/05/19 Python
python 将html转换为pdf的几种方法
2020/12/29 Python
详解css3 flex弹性盒自动铺满写法
2020/09/17 HTML / CSS
加拿大租车网站:Enterprise Rent-A-Car
2018/07/26 全球购物
暑期实习鉴定
2013/12/16 职场文书
关于旷工的检讨书
2014/02/02 职场文书
音乐专业自荐信
2014/02/07 职场文书
投标承诺书范本
2014/03/27 职场文书
环保倡议书500字
2014/05/15 职场文书
学校食品安全责任书
2015/01/29 职场文书
网聊搭讪开场白
2015/05/28 职场文书
微观世界观后感
2015/06/10 职场文书