html实现类似android的tab切换页面
2015-06-09 10:48
375 查看
前言:初学html,css,js。想做一个类似于android的tab切换的页面,从网上找了好多例子,只不过一旦子页面的div中加上一个div就起不到切换的效果了,所以一急之下,自己
根据隐藏,显示的方法,写了一个类似androidtab切换页面的例子,个人也不清楚这样做合不合理,写此篇文章一个是希望对新手童鞋有个启发,同时也希望知道怎么实现合
理的大大给我留言,让我能学习到更靠谱的方法(我这个是主要用在移动端的)。勿喷。不多说了,内容比较简单,直接贴代码:
html代码:
<body onload="loadon()">
<div id="mybody" class="mybody">
<div id="centertab" class="centertab">
<div onclick="onleft()" id="left" class="left">left</div>
<div onclick="onright()" id="right" class="right">right</div>
</div>
</div>
<div id="parent1"> 页面一</div>
<div id="parent2" style="display:none">页面二</div>
</body>
这种实现方法,在parent1和parent2中添加任何内容都可以,包括div,都不会影响到切换的效果。可能就是界面一开始加载的时候内容多,如果内容太多可能会影响用户体验。
CSS代码:
<style type="text/css">
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td { margin: 0; padding: 0;}
.mybody{
width:100%;
height:50px;
background-color:#000;
margin:0 auto;
}
.centertab{
width:200px;
margin:auto auto;
}
.left{
width:98px;
height:24px;
float:left;
background-color:#999;
text-align:center;
line-height:24px;
color:#000;
border:1px solid #999;
}
.right{
width:98px;
height:24px;
float:left;
background-color:#000;
text-align:center;
line-height:24px;
color:#FFF;
border:1px solid #999;
}
</style>
js代码:
<script type="text/javascript">
function loadon(){
var screenWidth = document.body.clientWidth;//获取body可见的宽度
document.getElementById("left").style.marginTop = 13+"px";
document.getElementById("right").style.marginTop = 13+"px";
}
function onleft(){
document.getElementById("parent2").style.display = "none";
document.getElementById("parent1").style.display = "block";
document.getElementById("left").style.backgroundColor="#999999";
document.getElementById("left").style.color="#000000";
document.getElementById("right").style.backgroundColor="#000000";
document.getElementById("right").style.color="#ffffff";
}
function onright(){
document.getElementById("parent2").style.display = "block";
document.getElementById("parent1").style.display = "none";
document.getElementById("left").style.backgroundColor="#000000";
document.getElementById("left").style.color="#ffffff";
document.getElementById("right").style.backgroundColor="#999999";
document.getElementById("right").style.color="#000000";
}
</script>
这里的onload方法在android的webview中用的时候获取的宽度有时候会不准,可以在webview加载完成是调用获取的方法比较靠谱。
希望有好方法实现的大大多多留言,指导指导。拜谢!
根据隐藏,显示的方法,写了一个类似androidtab切换页面的例子,个人也不清楚这样做合不合理,写此篇文章一个是希望对新手童鞋有个启发,同时也希望知道怎么实现合
理的大大给我留言,让我能学习到更靠谱的方法(我这个是主要用在移动端的)。勿喷。不多说了,内容比较简单,直接贴代码:
html代码:
<body onload="loadon()">
<div id="mybody" class="mybody">
<div id="centertab" class="centertab">
<div onclick="onleft()" id="left" class="left">left</div>
<div onclick="onright()" id="right" class="right">right</div>
</div>
</div>
<div id="parent1"> 页面一</div>
<div id="parent2" style="display:none">页面二</div>
</body>
这种实现方法,在parent1和parent2中添加任何内容都可以,包括div,都不会影响到切换的效果。可能就是界面一开始加载的时候内容多,如果内容太多可能会影响用户体验。
CSS代码:
<style type="text/css">
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td { margin: 0; padding: 0;}
.mybody{
width:100%;
height:50px;
background-color:#000;
margin:0 auto;
}
.centertab{
width:200px;
margin:auto auto;
}
.left{
width:98px;
height:24px;
float:left;
background-color:#999;
text-align:center;
line-height:24px;
color:#000;
border:1px solid #999;
}
.right{
width:98px;
height:24px;
float:left;
background-color:#000;
text-align:center;
line-height:24px;
color:#FFF;
border:1px solid #999;
}
</style>
js代码:
<script type="text/javascript">
function loadon(){
var screenWidth = document.body.clientWidth;//获取body可见的宽度
document.getElementById("left").style.marginTop = 13+"px";
document.getElementById("right").style.marginTop = 13+"px";
}
function onleft(){
document.getElementById("parent2").style.display = "none";
document.getElementById("parent1").style.display = "block";
document.getElementById("left").style.backgroundColor="#999999";
document.getElementById("left").style.color="#000000";
document.getElementById("right").style.backgroundColor="#000000";
document.getElementById("right").style.color="#ffffff";
}
function onright(){
document.getElementById("parent2").style.display = "block";
document.getElementById("parent1").style.display = "none";
document.getElementById("left").style.backgroundColor="#000000";
document.getElementById("left").style.color="#ffffff";
document.getElementById("right").style.backgroundColor="#999999";
document.getElementById("right").style.color="#000000";
}
</script>
这里的onload方法在android的webview中用的时候获取的宽度有时候会不准,可以在webview加载完成是调用获取的方法比较靠谱。
希望有好方法实现的大大多多留言,指导指导。拜谢!
相关文章推荐
- Android sdk content loader 0%的解决方案
- Android EventBus实战
- Android Ant打包及其环境变量配置
- Android 自定义View使用示例(二)
- android实习期日常笔记(二)
- Android GridView实现滚动到指定位置的方法
- 传智播客Android7.16日入驻大广州
- android学习之---基本问题1
- AndroidのListView包含CheckBox时滑动丢失选中状态的解决
- Android NDK (学习笔记五) —— java层和native层进行字符串的交互处理
- Android 利用sharepreference保存应用程序状态
- Android Design Support Library使用详解
- java.lang.ClassCastException: 。。。$LayoutParams cannot be cast to android.widget.LinearLayout$Layout
- Android调用系统相机拍照并获取图片
- android中Parcelable接口的使用
- Android 调用QQ登录
- android 自定义view 圆形进度条
- android开发环境安装与配置
- 如何获取Android唯一标识(唯一序列号)
- Android studio导入项目(出现的问题)