tab选择卡切换
2012-08-06 00:37
85 查看
很多页面中都会用到tab选项卡切换,下面这个巧妙的运用css设置边框,来使得激活的tab与其他未激活的边框有所不同。
就是将激活的边框的border-bottom设置为与激活tab的背景色一致,这样就看起来下面是没有边框的。
然后用jquery实现tab的切换,首先载入页面时就去掉所有的active样式,去掉所有的content内容,然后给第一个tab加上active样式,显示第一个content。然后点击对应的tab的时候,同样首先去掉所有的active样式,去掉所有的content内容,给当前点击的tab加active样式,然后通过获取当前点击tab的herf的内容来找到对应content的id并渐入显示。
就是将激活的边框的border-bottom设置为与激活tab的背景色一致,这样就看起来下面是没有边框的。
然后用jquery实现tab的切换,首先载入页面时就去掉所有的active样式,去掉所有的content内容,然后给第一个tab加上active样式,显示第一个content。然后点击对应的tab的时候,同样首先去掉所有的active样式,去掉所有的content内容,给当前点击的tab加active样式,然后通过获取当前点击tab的herf的内容来找到对应content的id并渐入显示。
Code <!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=utf-8" /> <title>tab选项卡</title> <script type="text/javascript" src="jquery-1.3.2.min.js" language="javascript"></script> <style> ul.tabs { margin: 0; padding: 0; float: left; list-style: none; height: 32px; /*--Set height of tabs--*/ border-bottom: 1px solid #999; border-left: 1px solid #999; width: 500px; } ul.tabs li { float: left; margin: 0; padding: 0; height: 31px; /*--Subtract 1px from the height of the unordered list--*/ line-height: 31px; /*--Vertically aligns the text within the tab--*/ border: 1px solid #999; border-left: none; margin-bottom: -1px; /*--Pull the list item down 1px--*/ overflow: hidden; position: relative; background: #e0e0e0; } ul.tabs li a { text-decoration: none; color: #000; display: block; font-size: 1.2em; padding: 0 20px; border: 1px solid #fff; /*--Gives the bevel look with a 1px white border inside the list item--*/ outline: none; } ul.tabs li a:hover { background: #ccc; } html ul.tabs li.active, html ul.tabs li.active a:hover { /*--Makes sure that the active tab does not listen to the hover properties--*/ background: #fff; border-bottom: 1px solid #fff; /*--Makes the active tab look like it's connected with its content--*/ } .tab_container { border: 1px solid #999; border-top: none; overflow: hidden; clear: both; float: left; width: 500px; background: #fff; } .tab_content { padding: 20px; font-size: 1.2em; } </style> <script> $(function(){ //进入页面时 $(".tab_content").hide(); //候隐藏所有的content内容 $("ul.tabs li:first").addClass("active").show(); //激活第一个tab $(".tab_content:first").show(); //显示第一个content内容 //点击对应的tab时 $("ul.tabs li").click(function() { $("ul.tabs li").removeClass("active"); //将所有的tab去掉active样式 $(this).addClass("active"); //点击的这个tab加active样式 $(".tab_content").hide(); //候隐藏所有的content内容 var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content $(activeTab).fadeIn(); //Fade in the active ID content }); }) </script> </head> <body> <div class="container"> <ul class="tabs"> <li><a href="#tab1">tab1</a></li> <li><a href="#tab2">tab2</a></li> <li><a href="#tab3">tab3</a></li> <li><a href="#tab4">tab4</a></li> <li><a href="#tab5">tab5</a></li> </ul> <div class="tab_container"> <div id="tab1" class="tab_content"> Content1 </div> <div id="tab2" class="tab_content"> Content2 </div> <div id="tab3" class="tab_content"> Content3 </div> <div id="tab4" class="tab_content"> Content4 </div> <div id="tab5" class="tab_content"> Content5 </div> </div> </div> </body> </html>
相关文章推荐
- tab选择卡切换
- RN 的scrollable-tab-view控件(同一页面选择切换)
- NO.16 tab选择卡切换
- Android中Tab的应用,底部,随选择切换图标和文字颜色
- 超简单tab切换--tablayout的使用
- tab微信主页切换1
- 如何在全屏游戏中禁用Alt+Tab等切换任务
- 【转】如何改变VC中控件间的TAB切换顺序
- Javascript实现Tab切换
- Android studio里有没有类似Eclipse中Tab键在输入函数参数时切换的功能
- jQuery之Tab切换代码改进
- tab切换效果
- PagerSlidingTabStripLib游标滑动切换标题
- 仿多页面滑动切换时背景指示图(如TAB文字下边的白条等)的动画实现原理,例PagerSlidingTabStrip
- jquery-easyui关闭tab自动切换到前一个tab
- android自定义控件之底部自动切换Tab布局
- 实现tablayout切换时颜色随之渐变的效果
- iOS AudioSession详解 Category选择 听筒扬声器切换kAudioSessionCategory_MediaPlayback
- jquery tab切换 标签切换插件——jQuery.fn.tabSwitch
- Android技术——视图切换(一)利用ActionBar实现Tab导航