html 代码:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>js-tabs</title>
<link rel="stylesheet" type="text/css" href="../css/base.css" media="all"/>
<style type="text/css">
a{color:#a0b3d6}
.tabs{border:1px solid #a0b3d6margin:100pxwidth:300px}
.tabs-nav a{background:#eaf0fdline-height:30pxpadding:0 20pxdisplay:inline-blockborder-right:1px solid #a0b3d6border-bottom:1px solid #a0b3d6float:left}
.tabs-nav .select1,.tabs-nav .select2,.tabs-nav .select3,.tabs-nav .select4{background:whiteborder-bottom:1px solid white_position:relative}
.tabs-content{padding:20pxborder-top:1px solid #a0b3d6margin-top:-1px}
</style>
</head>
<body>
<div class="tabs" id="tabs">
<h2 class="tabs-nav clearfix">
<a href="javascript:">首页</a>
<a href="javascript:">技术</a>
<a href="javascript:">生活</a>
<a href="javascript:">作品</a>
</h2>
<p class="tabs-content">首页首页首页首页首页首页首页首页首页首页</p>
<p class="tabs-content">技术技术技术技术技术技术技术技术技术技术</p>
<p class="tabs-content">生活生活生活生活生活生活生活生活生活生活</p>
<p class="tabs-content">作品作品作品作品作品作品作品作品作品作品</p>
</div>
<br/><br/>
<div class="tabs" id="tabs2">
<h2 class="tabs-nav clearfix">
<a href="javascript:">11111</a>
<a href="javascript:">22222</a>
<a href="javascript:">33333</a>
</h2>
<p class="tabs-content">11111111111111111111111111111111111</p>
<p class="tabs-content ">222222222222222222222222222222222222</p>
<p class="tabs-content ">333333333333333333333333333333333333333</p>
</div>
</body>
</html>
<script type="text/javascript" src="tabs.js"></script>
<script type="text/javascript">
window.onload = function(){
tabs('tabs','click',true,1000)
tabs('tabs2','mouseover')
}
</script>
tabs.js 代码:
function tabs(id,trigger,autoplay,time){
var tabsWrap = document.getElementById(id)
var tabsBtn = tabsWrap.getElementsByTagName('h2').getElementsByTagName('a')
var tabsContent = getClass('tabs-content',tabsWrap)
var timer = null
var current = 0
show(0)
for(var i = 0,len = tabsBtn.lengthi <leni++){
tabsBtn[i].index = i
if(trigger == 'click'){
tabsBtn[i].onclick = function(){
show(this.index)
}
}else if(trigger == 'mouseover'){
tabsBtn[i].onmouseover = function(){
show(this.index)
}
}
}
if(autoplay){
autoPlay()
tabsWrap.onmouseover = function(){
clearInterval(timer)
}
tabsWrap.onmouseout = function(){
autoPlay()
}
}
function autoPlay(){
timer = setInterval(function(){
show(current)
current++
if(current >= tabsBtn.length){
current = 0
}
},time)
}
function show(n){
current = n
for(var i = 0,len = tabsBtn.lengthi <leni++){
tabsBtn[i].className = ''
tabsContent[i].style.display = 'none'
}
tabsBtn[current].className = 'select' + (current + 1)
tabsContent[current].style.display = 'block'
}
function getClass(classname,obj){
var results = []
var elems = obj.getElementsByTagName('*')
for(var i = 0i <elems.lengthi++){
if(elems[i].className.indexOf(classname) != -1){
results[results.length] = elems[i]
}
}
return results
}
}
第一步,创建静态页面tabs.html,默认是HTML5模板,如下图所示:第二步,引入ionic相关的CSS文件和JS文件,如下图所示:
第三步,在body插入div和a标签元素,并设置样式,如下图所示:
第四步,预览该静态页面,这时会看到四个Tabs标签,如下图所示:
第五步,再次设置Tabs样式,使每个Tabs样式不一样,如下图所示:
第六步,下面插入AngularJS相关的指令,ng-app、ng-controlle指令,如下图所示: