登录  /  注册
首页 > web前端 > js教程 > 正文

JS实现仿Windows经典风格的选项卡Tab切换代码_javascript技巧

php中文网
发布: 2016-05-16 15:35:56
原创
1802人浏览过

本文实例讲述了js实现仿windows经典风格的选项卡tab切换代码。分享给大家供大家参考,具体如下:

这款仿Windows风格的选项卡,带有灰色的立体感,示例内容是用JS控制输出,只是为了演示功能,你在用的时候完全可以去掉的。

运行效果截图如下:

在线演示地址如下:

http://demo.jb51.net/js/2015/js-f-windows-style-tab-demo/

具体代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>选项卡</title>
<style type="text/css">
body{
 background-color:#CCC;
}
.tab{
 margin:0;
 padding:0;
 position:absolute;
}
.tab li{
 float:left;
 list-style:none;
}
.tab li span{
 float:left;
 display:block;
 height:20px;
 line-height:20px;
 position:relative;
 border-style:solid;
 border-width:1px 1px 0 1px;
 border-color:#EEE #999 #777 #DDD;
 background-color:#CCC;
 margin:1px 2px 0 0;
 padding:0 5px 0 5px;
 font-size:12px;
 cursor:pointer;
 z-index:1;
}
.tab li div{
 display:none;
 position:absolute;
 top:22px;
 left:0px;
 background-color:#CCC;
 border:1px solid;
 border-color:#EEE #999 #777 #DDD;
 text-align:center;
 overflow:auto;
}
.tab li.selected span{
 margin-top:0;
 height:22px;
}
.tab li.selected div{
 display:block;
}
</style>
<script type="text/javascript">
function CreateTab(tab_width,tab_height,parent_obj){
 var newtab=document.createElement("UL");
 newtab.className="tab";
 parent_obj.appendChild(newtab);
 Tab.call(newtab);
 newtab.style.width=tab_width+"px";
 newtab.style.height=tab_height+"px";
 return newtab;
}
function Tab(){
 var this_tab=this;
 this.selected_page;
 this.page_names=new Array();
 this.page_contents=new Array();
 this.Select=function(){
  this_tab.selected_page.className="";
  this.className="selected";
  this_tab.selected_page=this;
 }
 this.NewPage=function(page_name){
  var newpage=document.createElement("LI");
  newpage.onclick=this.Select;
  newpage.innerHTML="<span>"+page_name+"</span><div></div>";
  this.appendChild(newpage);
  newpage.lastChild.style.width=parseInt(this.style.width)-2+"px";
  newpage.lastChild.style.height=parseInt(this.style.height)-24+"px";
  this.page_names.push(newpage.firstChild);
  this.page_contents.push(newpage.lastChild);
  return newpage;
 }
 this.SetChecked=function(page_index){
  if(page_index>-1&&this.childNodes.length>page_index){
   this.selected_page=this.childNodes[page_index];
   this.selected_page.className="selected";
  }
 }
}
</script>
</head>
<body>
<script type="text/javascript">
var newtab=CreateTab(400,300,document.body);
 newtab.NewPage("第一页");
 newtab.NewPage("第二页");
 newtab.NewPage("第三页");
 newtab.NewPage("第四页");
 newtab.NewPage("第五页");
 newtab.SetChecked(0);
 newtab.style.top="100px";
 newtab.style.left="200px";
 for(var i=0;i<newtab.childNodes.length;i++){
  newtab.page_contents[i].innerHTML=function(number){
   var content_str="";
   for(var n=0;n<1;n++){
    content_str+="<br />---------------这是第"+number+"页---------------";
   }
   return content_str;
  }(i+1);
 }
</script>
</body>
</html>

登录后复制

希望本文所述对大家JavaScript程序设计有所帮助。

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号