在这有两种方法:
1.最常见的方法,也是mui的example中的代码
【思路】:在一个包含底部nav主页面(nav.html)中创建n个子页面(one.html,two.html,three.html),点击导航时通过对各个子页面的显隐(hide(),show())来控制展示。
代码如下
image.png
新建四个页面:
nav.html
one.html(首页)
two.html
three.html
来先把one.html,two.html,three.html三个需要公共导航的页面写好
nav.html页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello MUI</title>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" href="../../css/mui.css">
<style>
html,
body {
background-color: #efeff4;
}
</style>
</head>
<body>
<nav class="mui-bar mui-bar-tab">
<a id="defaultTab" class="mui-tab-item mui-active" href="one.html">
<span class="mui-icon mui-icon-home"></span>
<span class="mui-tab-label">one</span>
</a>
<a class="mui-tab-item" href="two.html">
<span class="mui-icon mui-icon-email"><span class="mui-badge">9</span></span>
<span class="mui-tab-label">two</span>
</a>
<a class="mui-tab-item" href="three.html">
<span class="mui-icon mui-icon-contact"></span>
<span class="mui-tab-label">three</span>
</a>
</nav>
<div class="mui-content">
</div>
<script src="../../js/mui.min.js"></script>
<script type="text/javascript" charset="utf-8">
//mui初始化
mui.init();
var subpages = ['one.html', 'two.html', 'three.html'];
var subpage_style = {
top: '0px',
bottom: '51px'
};
var aniShow = {};
//创建子页面,首个选项卡页面显示,其它均隐藏;
mui.plusReady(function() {
var self = plus.webview.currentWebview();
var len = subpages.length;
for (var i = 0; i < len; i++) {
var temp = {};
var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
if (i > 0) {
sub.hide();
}else{
temp[subpages[i]] = "true";
mui.extend(aniShow,temp);
}
self.append(sub);
}
});
//当前激活选项
var activeTab = subpages[0];
var title = document.getElementById("title");
//选项卡点击事件
mui('.mui-bar-tab').on('tap', 'a', function(e) {
var targetTab = this.getAttribute('href');
if (targetTab == activeTab) {
return;
}
//更换标题
// title.innerHTML = this.querySelector('.mui-tab-label').innerHTML;
//显示目标选项卡
//若为iOS平台或非首次显示,则直接显示
if(mui.os.ios||aniShow[targetTab]){
plus.webview.show(targetTab);
}else{
//否则,使用fade-in动画,且保存变量
var temp = {};
temp[targetTab] = "true";
mui.extend(aniShow,temp);
plus.webview.show(targetTab,"fade-in",300);
}
//隐藏当前;
plus.webview.hide(activeTab);
//更改当前活跃的选项卡
activeTab = targetTab;
});
//自定义事件,模拟点击“首页选项卡”
document.addEventListener('gohome', function() {
var defaultTab = document.getElementById("defaultTab");
//模拟首页点击
mui.trigger(defaultTab, 'tap');
//切换选项卡高亮
var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
if (defaultTab !== current) {
current.classList.remove('mui-active');
defaultTab.classList.add('mui-active');
}
});
</script>
</body>
</html>
2.对应复杂,多页面的实操方案
第一种方案存在的问题有,
1.当打开主页面时,(one.html,two.html,three.html)会全部进行渲染加载,如果你的子页面多且复杂,就会导致性能问题如加载速度慢甚至页面奔溃。
2.有些项目中存在页面权限的问题,它的每一个接口都会实时验证用户权限。我们需求的是,进入每一个子页面,都需要实时请求接口。
方案二目的在解决以上俩个问题
【思路】:在一个包含底部nav主页面中创建n个子页面,但是只创建首项子页面,点击导航时关闭之前的子页面,再创建当前要显示的子页面。
代码如下
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link href="../../css/mui.min.css" rel="stylesheet" />
</head>
<body>
<header class="mui-bar mui-bar-nav">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">标题</h1>
</header>
<div class="mui-content">
</div>
<nav class="mui-bar mui-bar-tab">
<a class="mui-tab-item mui-active" data-index="0">
<span class="mui-icon mui-icon-home"></span>
<span class="mui-tab-label">首页</span>
</a>
<a class="mui-tab-item" data-index="1">
<span class="mui-icon mui-icon-phone"></span>
<span class="mui-tab-label">电话</span>
</a>
<a class="mui-tab-item" data-index="2">
<span class="mui-icon mui-icon-email"></span>
<span class="mui-tab-label">邮件</span>
</a>
<a class="mui-tab-item" data-index="3">
<span class="mui-icon mui-icon-gear"></span>
<span class="mui-tab-label">设置</span>
</a>
</nav>
<script src="../../js/mui.min.js"></script>
<script type="text/javascript">
mui.init();
//要切换的子页面
var subpages = [
'head.html',
'controls.html',
'chat.html'];
//子页面样式,这里只有主页的底部是公用的,所以距离底部51个px,距离顶部0个px
var subpage_style = {
top : '50px',
bottom: '51px'
};
var self;
var nowsub;
//创建子页面,首个选项卡页面显示,其它均隐藏;
mui.plusReady(function() {
self = plus.webview.currentWebview();
nowsub = plus.webview.create(subpages[0], subpages[0], subpage_style);
self.append(nowsub);
});
mui('.mui-bar').on('tap','a',function(e){
var index_ = parseInt(this.attributes['data-index'].value);
reCreatePage(index_);
});
//导航栏中的菜单返回
mui.oldback = mui.back;
mui.back = function(){
if(nowsub.id!=subpages[0]){
//如果不是首项,则返回首项
reCreatePage(0);
}else{
mui.oldback();
}
}
function reCreatePage(index){
nowsub.close();
nowsub = plus.webview.create(subpages[index], subpages[index], subpage_style);
self.append(nowsub);
}
</script>
</body>
</html>
来啊喜欢就给我点个小心心哇O(∩_∩)O哈哈~
网友评论