如何使用jQuery技术开发ios风格的页面导航菜单


Posted in Javascript onJuly 29, 2015

效果图:

如何使用jQuery技术开发ios风格的页面导航菜单

如何使用jQuery技术开发ios风格的页面导航菜单

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

HTML代码

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

<nav>
<h1>导航菜单</h1>
<ul>
<li>
<h2>专题教程</h2>
<ul>
<li>
<h3>HTML专题教程</h3>
<ul>
<li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-introduction">GBin1专题之HTML5教程 - 第一篇:HTML5介绍</a></li>
<li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-new-elements">GBin1专题之HTML5教程 - 第二篇:HTML5元素</a></li>
<li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-video">GBin1专题之HTML5教程 - 第三篇:HTML5 Video元素</a></li>
<li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-video-doc">GBin1专题之HTML5教程 - 第四篇:HTML5 Video Doc</a></li>
<li><a href="http://www.gbin1.com/tutorials/html5-tutorial/html5-audio">GBin1专题之HTML5教程 - 第五篇:HTML5 Audio元素</a></li>
</ul>
<li>
<h3>GBin1热点秀</h3>
<ul>
<li><a href="http://www.gbin1.com/tutorials/hotspot/hotspot1/">GBin1专题之Web热点秀#1</a>
<li><a href="http://www.gbin1.com/tutorials/hotspot/hotspot2/">GBin1专题之Web热点秀#2</a>
<li><a href="http://www.gbin1.com/tutorials/hotspot/hotspot3/">GBin1专题之Web热点秀#3</a>
</ul>
 
</ul>

。。。 。。。
 
Javascript

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

$(function(){
 
var nav = $("nav"),
navTitle = nav.children().first(),
navTitleLabel = navTitle.text(),
mainList = navTitle.next(),
subLevels = mainList.find("ul"),
hiddenClass = "hidden";
 
nav.addClass("js");
mainList.find("a:last-child").addClass("files");
subLevels.addClass(hiddenClass);
 
navTitle.wrap($("<div/>")).before($("<a/>", {
href: "#",
className: hiddenClass,
click: function(e){
var $this = $(this),
activeList = subLevels.filter(":visible:last"),
activeListParents = activeList.parents("ul");
navTitle.text($this.text());
if(activeListParents.length > 2)
$this.text(activeListParents.eq(1).prev().text());
else if (activeListParents.length > 1)
$this.text(navTitleLabel)
else
$this.addClass(hiddenClass).empty();
setTimeout(
function(){
activeList.addClass(hiddenClass);
}, slideDuration - 100
);
mainList.css("left", parseInt(mainList.css("left")) + navWidth + "px");
e.preventDefault();
}
}));
 
subLevels.prev().wrap($("<a/>", {
href:"#",
click: function(e){
var $this = $(this);
backArrow.removeClass(hiddenClass).text(navTitle.text());
navTitle.text($this.text());
$this.next().removeClass(hiddenClass);
mainList.css("left", parseInt(mainList.css("left")) - navWidth + "px");
e.preventDefault();
}
}));
 
var backArrow = navTitle.prev(),
navWidth = nav.width(),
firstSubLevel = subLevels.eq(0),
docStyle = document.documentElement.style,
slideDuration = 0,
timingRatio = 1000;
 
if(docStyle.WebkitTransition !== undefined)
slideDuration = parseFloat(
firstSubLevel.css("-webkit-transition-duration")
) * timingRatio;
 
if(docStyle.MozTransition !== undefined)
slideDuration = parseFloat(
firstSubLevel.css("-moz-transition-duration")
) * timingRatio;
 
if(docStyle.OTransition !== undefined)
slideDuration = parseFloat(
firstSubLevel.css("-o-transition-duration")
) * timingRatio;
 
});

 
CSS

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

body {
font-size: 14px;
font-family: Arial;
background:#f5f5f8;
}
.js {
position:absolute;
width:320px;
height:480px;
top:50%;
left:50%;
margin:-280px 0 0 -160px;
overflow:hidden;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
background:#fff;
-webkit-box-shadow:0 1px 2px rgba(0,0,0,.25);
-moz-box-shadow:0 1px 2px rgba(0,0,0,.25);
box-shadow:0 1px 2px rgba(0,0,0,.25);
}
.js .files {
background-image:none;
}
.js .hidden {
display:none;
}
.js * {
font-size:14px;
font-weight:400;
margin:0;
padding:0;
list-style:none;
}
.js > div {
position:relative;
z-index:1;
height:44px;
text-align:center;
font-size:14px;
line-height:44px;
color:#fff;
text-shadow:0 -1px 0 rgba(0,0,0,.4);
background:#7f94b0;
background:-webkit-gradient(
linear,
0 0,
0 100%,
color-stop(0,#b5c0ce),
color-stop(0.5,#889bb3),
color-stop(0.51,#7f94b0),
color-stop(1,#6d83a1)
);
background:-moz-linear-gradient(
top center,
#b5c0ce,
#889bb3 22px,
#7f94b0 23px,
#6d83a1
);
border-bottom:1px solid #2d3033;
-webkit-border-top-left-radius:5px;
-webkit-border-top-right-radius:5px;
-moz-border-radius-topleft:5px;
-moz-border-radius-topright:5px;
border-top-left-radius:5px;
border-top-right-radius:5px;
}
.js > div a {
height:31px;
top:7px;
left:20px;
font-size:14px;
line-height:31px;
color:#fff;
background:url('../images//center.png');
}
.js > div a, .js > div a:before, .js > div a:after {
position:absolute;
}
.js > div a:before {
left:-13px;
content:url('../images//left.png');
}
.js > div a:after {
right:-10px;
top:0;
content:url('../images//right.png');
}
.js > div a:active {
opacity:.65;
}
.js a {
text-decoration:none;
}
.js ul a {
display:block;
color:#000;
padding:.8em 18px;
border-bottom:1px solid #e0e0e0;
background:url('images/next.png') no-repeat 94% 50%;
}
.js ul a:hover {
background-color:#edf3fe;
}
.js a:focus {
outline:none;
}
.js ul {
position:absolute;
top:0;
padding-top:45px;
width:100%;
-webkit-transition:left .4s ease-out;
-moz-transition:left .4s ease-out;
-o-transition:left .4s ease-out;
}
.js ul {
left:0;
}
.js ul ul {
left:320px;
}

有效果图,有代码看起来非常明了,希望大家喜欢。

Javascript 相关文章推荐
JavaScript flash复制库类 Zero Clipboard
Jan 17 Javascript
jQuery表单验证插件formValidator(改进版)
Feb 03 Javascript
JS跨域问题详解
Nov 25 Javascript
Node.js的包详细介绍
Jan 14 Javascript
JavaScript原生对象之String对象的属性和方法详解
Mar 13 Javascript
JS设置网页图片vspace和hspace属性的方法
Apr 01 Javascript
老生常谈combobox和combotree模糊查询
Apr 17 Javascript
Vue shopCart 组件开发详解
Jan 26 Javascript
javascript对HTML字符转义与反转义
Dec 13 Javascript
浅谈redux, koa, express 中间件实现对比解析
May 23 Javascript
vue 查看dist文件里的结构(多种方式)
Jan 17 Javascript
vue 实现弹窗关闭后刷新效果
Apr 08 Vue.js
如何用javascript计算文本框还能输入多少个字符
Jul 29 #Javascript
详解JavaScript的Polymer框架中的通知交互
Jul 29 #Javascript
JavaScript的Polymer框架中dom-repeat与VM的相关操作
Jul 29 #Javascript
浅谈JavaScript的Polymer框架中的事件绑定
Jul 29 #Javascript
探讨JavaScript中的Rest参数和参数默认值
Jul 29 #Javascript
JS的框架Polymer中的dom-if和is属性使用说明
Jul 29 #Javascript
js数组去重的方法汇总
Jul 29 #Javascript
You might like
yii操作cookie实例简介
2014/07/09 PHP
PHP+AJAX实现投票功能的方法
2015/09/28 PHP
JavaScript 学习笔记(十三)Dom创建表格
2010/01/21 Javascript
利用js制作html table分页示例(js实现分页)
2014/04/25 Javascript
js图片自动轮播代码分享(js图片轮播)
2014/05/06 Javascript
jQuery+jsp实现省市县三级联动效果(附源码)
2015/12/03 Javascript
JS实现全屏的四种写法
2016/12/30 Javascript
详解Vue中过度动画效果应用
2017/05/25 Javascript
基于jQuery实现定位导航位置效果
2017/11/15 jQuery
微信小程序 数据缓存实现方法详解
2019/08/26 Javascript
layui checkbox默认选中,获取选中值,清空所有选中项的例子
2019/09/02 Javascript
浅谈JavaScript中this的指向问题
2020/07/28 Javascript
JS如何监听div的resize事件详解
2020/12/03 Javascript
Python采用socket模拟TCP通讯的实现方法
2014/11/19 Python
python中Flask框架简单入门实例
2015/03/21 Python
python从入门到精通(DAY 3)
2015/12/20 Python
Python字符串特性及常用字符串方法的简单笔记
2016/01/04 Python
windows下安装Python和pip终极图文教程
2017/03/05 Python
Python 多线程的实例详解
2017/09/07 Python
使用python3构建文件传输的方法
2019/02/13 Python
Python中按值来获取指定的键
2019/03/04 Python
如何使用Python 打印各种三角形
2019/06/28 Python
canvas之万花筒效果的简单实现(推荐)
2016/08/16 HTML / CSS
美国零售商店:Blue&Cream
2017/04/07 全球购物
意大利婴儿产品网上商店:Mukako
2018/10/14 全球购物
MATCHESFASHION.COM美国官网:英国奢侈品零售商
2018/10/29 全球购物
英国最大最好的无人机商店:Drones Direct
2019/07/12 全球购物
会计学专业学生的求职信范文
2014/01/27 职场文书
学校大课间活动方案
2014/01/30 职场文书
幼儿园保教管理制度
2014/02/03 职场文书
党的群众路线教育学习材料
2014/05/12 职场文书
群众路线剖析材料怎么写
2014/10/09 职场文书
入党积极分子自我批评思想汇报
2014/10/10 职场文书
公司感恩节活动策划书
2014/10/11 职场文书
看古人们是如何赞美老师的?
2019/07/08 职场文书
【超详细】八大排序算法的各项比较以及各自特点
2021/03/31 Python