很棒的js选项卡切换效果

时间:2021-05-18

本文实例为大家分享了js实现简单的选项卡切换效果的具体代码,供大家参考,具体内容如下

js选项卡切换代码:

<!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title><link rel="stylesheet" type="text/css" href="base.css" media="all"/><style type="text/css">a{color:#a0b3d6;}.tabs{border:1px solid #a0b3d6;margin:100px;width:300px;}.tabs-nav a{background:#eaf0fd;line-height:30px;padding:0 20px;display:inline-block;border-right:1px solid #a0b3d6;border-bottom:1px solid #a0b3d6;float:left;}.tabs-nav .on{background:white;border-bottom:1px solid white;_position:relative;}.tabs-content{padding:20px;border-top:1px solid #a0b3d6;margin-top:-1px;}</style></head><body><div class="tabs" id="tabs"><h2 class="tabs-nav clearfix"><a href="javascript:;" class="on">首页</a><a href="javascript:;">技术</a><a href="javascript:;">生活</a><a href="javascript:;">作品</a></h2><p class="tabs-content">首页首页首页首页首页首页首页首页首页首页</p><p class="tabs-content hide">技术技术技术技术技术技术技术技术技术技术</p><p class="tabs-content hide">生活生活生活生活生活生活生活生活生活生活</p><p class="tabs-content hide">作品作品作品作品作品作品作品作品作品作品</p></div><br/><br/><div class="tabs" id="tabs2"><h2 class="tabs-nav clearfix"><a href="javascript:;" class="on">11111</a><a href="javascript:;">22222</a><a href="javascript:;">33333</a></h2><p class="tabs-content">11111111111111111111111111111111111</p><p class="tabs-content hide">222222222222222222222222222222222222</p><p class="tabs-content hide">333333333333333333333333333333333333333</p></div></body></html><script type="text/javascript" src="tabs.js"></script><script type="text/javascript">window.onload = function(){tabs('tabs','click');tabs('tabs2','mouseover');}</script> </body></html>

Tabs.js

function tabs(id,trigger){var tabsBtn = document.getElementById(id).getElementsByTagName('h2')[0].getElementsByTagName('a');var tabsContent = document.getElementById(id).getElementsByTagName('p');for(var i = 0,len = tabsBtn.length; i < len; i++){tabsBtn[i].index = i;if(trigger == 'click'){tabsBtn[i].onclick = function(){clearClass();this.className = 'on';showContent(this.index);}}else if(trigger == 'mouseover'){tabsBtn[i].onmouseover = function(){clearClass();this.className = 'on';showContent(this.index);}}}function showContent(n){for(var i = 0,len = tabsBtn.length; i < len; i++){tabsContent[i].className = 'hide';}tabsContent[n].className = 'tabs-content';}function clearClass(){for(var i = 0,len = tabsBtn.length; i < len; i++){tabsBtn[i].className = '';}}}

base.css

@charset "utf-8";html {color:black;background:white;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section {margin:0;padding:0;}body,button,input,select,textarea {font:12px SimSun,tahoma,arial,sans-serif;}input,select,textarea {font-size:100%;}table {border-collapse:collapse;border-spacing:0;}th {text-align:inherit;}fieldset,img {border:none;}iframe {display:block;}abbr,acronym {border:none;font-variant:normal;}del {text-decoration:line-through;}address,caption,cite,code,dfn,em,th,var {font-style:normal;font-weight:500;}ol,ul {list-style:none;}caption,th {text-align:left;}h1,h2,h3,h4,h5,h6 {font-size:100%;font-weight:500;}q:before,q:after {content:'';}sub, sup {font-size:75%;line-height:0;position:relative;vertical-align:baseline;}sup {top:-0.5em;}sub {bottom:-0.25em;}a:hover {text-decoration:underline;}ins,a {text-decoration:none;}a:focus,*:focus {outline:none;}.clearfix:before,.clearfix:after {content:"";display:table;}.clearfix:after {clear:both;overflow:hidden;}.clearfix {zoom:1; }.clear{clear:both;display:block;overflow:hidden;height:0;line-height:0;font-size:0;}.hide {display:none !important;visibility:hidden;}.block {display:block !important;}.fl {float:left;display:inline;}.fr {float:right;display:inline;}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章