很棒的js選項卡切換效果
更新時間:2016年07月15日 17:26:13 作者:kakaxiD
很棒的JavaScript選項卡切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)簡單的選項卡切換效果的具體代碼,供大家參考,具體內(nèi)容如下
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:;">技術(shù)</a> <a href="javascript:;">生活</a> <a href="javascript:;">作品</a> </h2> <p class="tabs-content">首頁首頁首頁首頁首頁首頁首頁首頁首頁首頁</p> <p class="tabs-content hide">技術(shù)技術(shù)技術(shù)技術(shù)技術(shù)技術(shù)技術(shù)技術(shù)技術(shù)技術(shù)</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"; /* @名稱: base @功能: 重設(shè)瀏覽器默認樣式 */ /* 防止用戶自定義背景顏色對網(wǎng)頁的影響,添加讓用戶可以自定義字體 */ html { color:black; background:white; } /* 內(nèi)外邊距通常讓各個瀏覽器樣式的表現(xiàn)位置不同 */ 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; } /* 要注意表單元素并不繼承父級 font 的問題 */ body,button,input,select,textarea { font:12px SimSun,tahoma,arial,sans-serif; } input,select,textarea { font-size:100%; } /* 去掉各Table cell 的邊距并讓其邊重合 */ table { border-collapse:collapse; border-spacing:0; } /* IE bug fixed: th 不繼承 text-align*/ th { text-align:inherit; } /* 去除默認邊框 */ fieldset,img { border:none; } /* ie6 7 8(q) bug 顯示為行內(nèi)表現(xiàn) */ iframe { display:block; } /* 去掉 firefox 下此元素的邊框 */ abbr,acronym { border:none; font-variant:normal; } /* 一致的 del 樣式 */ del { text-decoration:line-through; } address,caption,cite,code,dfn,em,th,var { font-style:normal; font-weight:500; } /* 去掉列表前的標識, li 會繼承 */ ol,ul { list-style:none; } /* 對齊是排版最重要的因素, 別讓什么都居中 */ caption,th { text-align:left; } /* 來自yahoo, 讓標題都自定義, 適應(yīng)多個系統(tǒng)應(yīng)用 */ h1,h2,h3,h4,h5,h6 { font-size:100%; font-weight:500; } q:before,q:after { content:''; } /* 統(tǒng)一上標和下標 */ sub, sup { font-size:75%; line-height:0; position:relative; vertical-align:baseline; } sup { top:-0.5em; } sub { bottom:-0.25em; } /* 讓鏈接在 hover 狀態(tài)下顯示下劃線 */ a:hover { text-decoration:underline; } /* 默認不顯示下劃線,保持頁面簡潔 */ ins,a { text-decoration:none; } /* IE6,7焦點點狀線去除 */ a:focus,*:focus { outline:none; } /* 清除浮動 */ .clearfix:before,.clearfix:after { content:""; display:table; } .clearfix:after { clear:both; overflow:hidden; } .clearfix { zoom:1; /* for IE6 IE7 */ } .clear{ clear:both; display:block; overflow:hidden; height:0; line-height:0; font-size:0; } /* 設(shè)置顯示和隱藏, 通常用來與 JS 配合 */ .hide { display:none !important; visibility:hidden; } .block { display:block !important; } /* 設(shè)置內(nèi)聯(lián), 減少浮動帶來的bug */ .fl { float:left; display:inline; } .fr { float:right; display:inline; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中Location.search處理使用方法
本文主要介紹了JavaScript中Location.search處理使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04深入了解Hybrid App技術(shù)的相關(guān)知識
這篇文章主要介紹了深入了解Hybrid App技術(shù)的相關(guān)知識,Hybrid App(混合模式移動應(yīng)用)是指介于web-app、native-app這兩者之間的app,兼具" Native App良好用戶交互體驗的優(yōu)勢 "和" Web App跨平臺開發(fā)的優(yōu)勢 ",需要的朋友可以參考下2019-07-07js 數(shù)組實現(xiàn)一個類似ruby的迭代器
今天突然發(fā)現(xiàn)js的數(shù)組處理起來真是麻煩,代碼一些就是一大堆,相比起ruby的迭代器來真是遜色不少。2009-10-10