当前位置: 首页 > news >正文

使用jQuery开发iOS风格的页面导航菜单

 
申请达人,去除赞助商链接

iOS风格的操作系统和导航方式现在越来越流行,在今天的jQuery教程中,我们将介绍如何生成一个iphone风格的菜单导航。

HTML代码

我们使用镶嵌的<li>来生成菜单内容,并且包含在<nav>标签中,如下:

  1. <nav>
  2. <h1>导航菜单</h1>
  3. <ul>
  4. <li>
  5. <h2>专题教程</h2>
  6. <ul>
  7. <li>
  8. <h3>HTML专题教程</h3>
  9. <ul>
  10. <li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-introduction">GBin1专题之HTML5教程 - 第一篇:HTML5介绍</a></li>
  11. <li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-new-elements">GBin1专题之HTML5教程 - 第二篇:HTML5元素</a></li>
  12. <li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-video">GBin1专题之HTML5教程 - 第三篇:HTML5 Video元素</a></li>
  13. <li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-video-doc">GBin1专题之HTML5教程 - 第四篇:HTML5 Video Doc</a></li>
  14. <li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-audio">GBin1专题之HTML5教程 - 第五篇:HTML5 Audio元素</a></li>
  15. </ul>
  16. <li>
  17. <h3>GBin1热点秀</h3>
  18. <ul>
  19. <li><a href="http://www.gbin1.com/tutorials/hotspot/hotspot1/">GBin1专题之Web热点秀#1</a>
  20. <li><a href="http://www.gbin1.com/tutorials/hotspot/hotspot2/">GBin1专题之Web热点秀#2</a>
  21. <li><a href="http://www.gbin1.com/tutorials/hotspot/hotspot3/">GBin1专题之Web热点秀#3</a>
  22. </ul>
  23.  
  24. </ul>
  25. 。。。 。。。
 
 

 

Javascript

使用jQuery来查询遍历li,并且生成菜单项目,如下:

  1. $(function(){
  2.  
  3. var nav = $("nav"),
  4. navTitle = nav.children().first(),
  5. navTitleLabel = navTitle.text(),
  6. mainList = navTitle.next(),
  7. subLevels = mainList.find("ul"),
  8. hiddenClass = "hidden";
  9.  
  10. nav.addClass("js");
  11. mainList.find("a:last-child").addClass("files");
  12. subLevels.addClass(hiddenClass);
  13.  
  14. navTitle.wrap($("<div/>")).before($("<a/>", {
  15. href: "#",
  16. className: hiddenClass,
  17. click: function(e){
  18. var $this = $(this),
  19. activeList = subLevels.filter(":visible:last"),
  20. activeListParents = activeList.parents("ul");
  21. navTitle.text($this.text());
  22. if(activeListParents.length > 2)
  23. $this.text(activeListParents.eq(1).prev().text());
  24. else if (activeListParents.length > 1)
  25. $this.text(navTitleLabel)
  26. else
  27. $this.addClass(hiddenClass).empty();
  28. setTimeout(
  29. function(){
  30. activeList.addClass(hiddenClass);
  31. }, slideDuration - 100
  32. );
  33. mainList.css("left", parseInt(mainList.css("left")) + navWidth + "px");
  34. e.preventDefault();
  35. }
  36. }));
  37.  
  38. subLevels.prev().wrap($("<a/>", {
  39. href:"#",
  40. click: function(e){
  41. var $this = $(this);
  42. backArrow.removeClass(hiddenClass).text(navTitle.text());
  43. navTitle.text($this.text());
  44. $this.next().removeClass(hiddenClass);
  45. mainList.css("left", parseInt(mainList.css("left")) - navWidth + "px");
  46. e.preventDefault();
  47. }
  48. }));
  49.  
  50. var backArrow = navTitle.prev(),
  51. navWidth = nav.width(),
  52. firstSubLevel = subLevels.eq(0),
  53. docStyle = document.documentElement.style,
  54. slideDuration = 0,
  55. timingRatio = 1000;
  56.  
  57. if(docStyle.WebkitTransition !== undefined)
  58. slideDuration = parseFloat(
  59. firstSubLevel.css("-webkit-transition-duration")
  60. ) * timingRatio;
  61.  
  62. if(docStyle.MozTransition !== undefined)
  63. slideDuration = parseFloat(
  64. firstSubLevel.css("-moz-transition-duration")
  65. ) * timingRatio;
  66.  
  67. if(docStyle.OTransition !== undefined)
  68. slideDuration = parseFloat(
  69. firstSubLevel.css("-o-transition-duration")
  70. ) * timingRatio;
  71.  
  72. });
 

 

CSS

使用图片来生成页面顶端的按钮:

  1. body {
  2. font-size: 14px;
  3. font-family: Arial;
  4. background:#f5f5f8;
  5. }
  6. .js {
  7. position:absolute;
  8. width:320px;
  9. height:480px;
  10. top:50%;
  11. left:50%;
  12. margin:-280px 0 0 -160px;
  13. overflow:hidden;
  14. -webkit-border-radius:5px;
  15. -moz-border-radius:5px;
  16. border-radius:5px;
  17. background:#fff;
  18. -webkit-box-shadow:0 1px 2px rgba(0,0,0,.25);
  19. -moz-box-shadow:0 1px 2px rgba(0,0,0,.25);
  20. box-shadow:0 1px 2px rgba(0,0,0,.25);
  21. }
  22. .js .files {
  23. background-image:none;
  24. }
  25. .js .hidden {
  26. display:none;
  27. }
  28. .js * {
  29. font-size:14px;
  30. font-weight:400;
  31. margin:0;
  32. padding:0;
  33. list-style:none;
  34. }
  35. .js > div {
  36. position:relative;
  37. z-index:1;
  38. height:44px;
  39. text-align:center;
  40. font-size:14px;
  41. line-height:44px;
  42. color:#fff;
  43. text-shadow:0 -1px 0 rgba(0,0,0,.4);
  44. background:#7f94b0;
  45. background:-webkit-gradient(
  46. linear,
  47. 0 0,
  48. 0 100%,
  49. color-stop(0,#b5c0ce),
  50. color-stop(0.5,#889bb3),
  51. color-stop(0.51,#7f94b0),
  52. color-stop(1,#6d83a1)
  53. );
  54. background:-moz-linear-gradient(
  55. top center,
  56. #b5c0ce,
  57. #889bb3 22px,
  58. #7f94b0 23px,
  59. #6d83a1
  60. );
  61. border-bottom:1px solid #2d3033;
  62. -webkit-border-top-left-radius:5px;
  63. -webkit-border-top-right-radius:5px;
  64. -moz-border-radius-topleft:5px;
  65. -moz-border-radius-topright:5px;
  66. border-top-left-radius:5px;
  67. border-top-right-radius:5px;
  68. }
  69. .js > div a {
  70. height:31px;
  71. top:7px;
  72. left:20px;
  73. font-size:14px;
  74. line-height:31px;
  75. color:#fff;
  76. background:url('../images//center.png');
  77. }
  78. .js > div a, .js > div a:before, .js > div a:after {
  79. position:absolute;
  80. }
  81. .js > div a:before {
  82. left:-13px;
  83. content:url('../images//left.png');
  84. }
  85. .js > div a:after {
  86. right:-10px;
  87. top:0;
  88. content:url('../images//right.png');
  89. }
  90. .js > div a:active {
  91. opacity:.65;
  92. }
  93. .js a {
  94. text-decoration:none;
  95. }
  96. .js ul a {
  97. display:block;
  98. color:#000;
  99. padding:.8em 18px;
  100. border-bottom:1px solid #e0e0e0;
  101. background:url('images/next.png') no-repeat 94% 50%;
  102. }
  103. .js ul a:hover {
  104. background-color:#edf3fe;
  105. }
  106. .js a:focus {
  107. outline:none;
  108. }
  109. .js ul {
  110. position:absolute;
  111. top:0;
  112. padding-top:45px;
  113. width:100%;
  114. -webkit-transition:left .4s ease-out;
  115. -moz-transition:left .4s ease-out;
  116. -o-transition:left .4s ease-out;
  117. }
  118. .js ul {
  119. left:0;
  120. }
  121. .js ul ul {
  122. left:320px;
  123. }
 

 

搞定! 请参考在线演示查看效果,希望大家喜欢这个简单的效果!

 

 

相关文章:

  • rsh 服务
  • Putty退出全屏
  • [Node + Docker] 聊聊怎么把 nodeclub 构建成 Docker 镜像
  • AS3编码规范(转)
  • 我的Android进阶之旅------ListView中android:cacheColorHint,android:listSelector属性作用 .
  • 技术人员在客户现场工作的注意事项
  • HDU 1520 Anniversary party
  • Android 计时应用 之 爱相随 V0.25
  • 杭电3790--最短路径问题(双权Dijkstra)
  • iostream迭代器的使用(11.18)
  • delphi 图像处理 二值化
  • 6个简单的解决方案解决Internet Explorer中的透明度问题
  • Atom飞行手册翻译: 3.5 创建主题
  • RMAN的基本概念和常用命令
  • 《go语言程序设计》学习(七)
  • C++入门教程(10):for 语句
  • CSS3 变换
  • Docker下部署自己的LNMP工作环境
  • Go 语言编译器的 //go: 详解
  • Javascript Math对象和Date对象常用方法详解
  • JS专题之继承
  • Promise面试题2实现异步串行执行
  • SpringCloud集成分布式事务LCN (一)
  • VUE es6技巧写法(持续更新中~~~)
  • Vue学习第二天
  • windows下如何用phpstorm同步测试服务器
  • 测试如何在敏捷团队中工作?
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 前端性能优化——回流与重绘
  • 事件委托的小应用
  • 我看到的前端
  • 我与Jetbrains的这些年
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (SpringBoot)第二章:Spring创建和使用
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (六)vue-router+UI组件库
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .NET程序员迈向卓越的必由之路
  • ;号自动换行
  • @软考考生,这份软考高分攻略你须知道
  • [8481302]博弈论 斯坦福game theory stanford week 1
  • [Apio2012]dispatching 左偏树
  • [C#]无法获取源 https://api.nuge t.org/v3-index存储签名信息解决方法
  • [C#C++]类CLASS