国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery+css實(shí)現(xiàn)Tab欄切換的代碼實(shí)例

2019-11-19 11:34:29
字體:
供稿:網(wǎng)友

前幾天面試碰到現(xiàn)場(chǎng)給寫一個(gè)Tab欄切換的功能,思想基本上懂,但是好久沒寫過,一時(shí)要全部實(shí)現(xiàn)效果還真有點(diǎn)難。回來后,再把思路理一理,寫一個(gè),基礎(chǔ)還是很重要的。

最終要實(shí)現(xiàn)的效果圖如下:

(1)點(diǎn)擊tab欄顯示對(duì)應(yīng)的內(nèi)容,并且tab欄樣式變化。實(shí)現(xiàn)方式:一般tab欄如果要做成比較好看的樣式,會(huì)切兩張圖作為背景,一張用于選中時(shí)的背景,一張用于未選中的背景。這里為了簡(jiǎn)單,只用css設(shè)置樣式。然后為每個(gè)tab綁定click事件,當(dāng)觸發(fā)click事件時(shí),對(duì)應(yīng)的內(nèi)容div的display設(shè)置block,否則設(shè)置為none。

(2)當(dāng)鼠標(biāo)懸停在沒有選中的tab欄上時(shí),改變樣式,移開時(shí)又恢復(fù)回來的樣式。如果tab欄已選中,則鼠標(biāo)是否懸停不影響樣式。實(shí)現(xiàn)方式:為tab欄添加hover事件,當(dāng)鼠標(biāo)進(jìn)入時(shí),判斷該tab欄是不是被選中(可以為了選中的tab欄設(shè)置一個(gè)class,只需要判斷是否含有該class即可),在不選中的情況下再添加hover的樣式。

完整代碼如下(代碼下載地址):

HTML:

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" /> <title>tab標(biāo)簽</title> <link href="css/style.css" rel="external nofollow" type="text/css" rel="stylesheet"></head><body> <div class="tab-contain"> <!-- tab欄 --> <ul id="tabs">  <li class="current"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="tab1">One</a></li>  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="tab2">Two</a></li>  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="tab3">Three</a></li>  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="tab4">Four</a></li> </ul> <!-- 對(duì)應(yīng)顯示內(nèi)容 --> <div id="content">  <div id="tab1" class="item show">  <h2>title 11111</h2>  <p>text here!!!text here!!!text here!!!text here!!!text here!!!</p>  <p>text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!</p>  </div>  <div id="tab2" class="item">  <h2>title 2222</h2>  <p>text here!!!text here!!!text here!!</p>  <p>text here!!!text here!!!text here!!!text here!text here!!!text here!!!text here!!!</p>  </div>  <div id="tab3" class="item">  <h2>title 33333</h2>  <p>text here!!!</p>  <p>text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!text here!!!</p>  </div>  <div id="tab4" class="item">  <h2>title 44444</h2>  <p>text here!!!text here!!!text here!!!text here!!!text here!!!</p>  <p>text here!!!text </p>  </div> </div> </div> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript" src="js.js"></script></body></html>

CSS:

.tab-contain{ margin: 50px auto; width: 1000px; padding:100px; background: #7F7D7D;}#tabs { overflow: hidden; width: 100%; margin: 0; padding: 0; list-style: none;}#tabs li { float: left; margin: 0;}li a { position: relative; background: #ddd; padding: 10px 50px; float: left; text-decoration: none; color: #444; text-shadow: 0 1px 0 rgba(255, 255, 255, .8); border-radius: 20px 20px 0 0; box-shadow: 0 2px 2px rgba(0, 0, 0, .4);} .current a{ outline: 0; background: #fff;  z-index: 4;}.hoverItem a{ background: #AAC8B9;}#content { background: #fff; padding: 50px; height: 220px; position: relative; border-radius: 0 5px 5px 5px; box-shadow: 0 -2px 3px -2px rgba(0, 0, 0, .5);}.item{ display: none;}.show{ display: block;}

JS:

 $(function(){  $('#tabs a').click(function(e) {   e.preventDefault();       $('#tabs li').removeClass("current").removeClass("hoverItem");   $(this).parent().addClass("current");   $("#content div").removeClass("show");   $('#' + $(this).attr('title')).addClass('show');  });  $('#tabs a').hover(function(){  if(!$(this).parent().hasClass("current")){   $(this).parent().addClass("hoverItem");  }  },function(){  $(this).parent().removeClass("hoverItem");  }); });

以上所述是小編給大家介紹的jquery css實(shí)現(xiàn)Tab欄切換詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 雷波县| 墨竹工卡县| 松桃| 镇巴县| 昭觉县| 靖江市| 类乌齐县| 潞城市| 扶余县| 洮南市| 信丰县| 安乡县| 新巴尔虎右旗| 金溪县| 西安市| 慈溪市| 清远市| 敖汉旗| 大冶市| 年辖:市辖区| 玉田县| 淮阳县| 峨山| 乌兰浩特市| 宁陵县| 临泉县| 松原市| 略阳县| 望城县| 昔阳县| 蕉岭县| 津南区| 璧山县| 建昌县| 商城县| 神农架林区| 中宁县| 福海县| 门源| 林州市| 闽清县|