DIV CSS 佈局教程網

不規則造型的 TAB 選項卡:把方正邊角換成圓角的實作
編輯:關於JavaScript     

本文這份選項卡與常見的方角版本差別只在視覺:把中規中矩的直角換成不規則的圓角造型,瀏覽者看一眼就覺得新鮮。技術上仍然是最直白的 display 與 color 切換,很適合當作選項卡入門的第一個練習。

執行結果如下:

線上展示:https://demo.jb51.net/js/2015/js-box-tab-nav-style-menu-codes/

造型的來源

兩個分頁各自有一張背景圖:news_bghover1.gif 與 news_bghover2.gif,尺寸 212 × 29。圖片本身自帶圓角與陰影,容器 .xixi1/.xixi2 只負責換背板。文字層 .tab1 寬 96px、.tab2 寬 116px,寬度不同是為了讓兩個標籤在視覺上占據合適的比例,這點小細節很值得學。

切換函式

setTab03Syn(i) 呼叫 selectTab03Syn(i),後者用 switch 處理兩個 case:case 1 顯示 TabCon1、隱藏 TabCon2,並把 font1 設為白字、font2 設為黑字;case 2 反之。onMouseOver 裡同時做了兩件事——呼叫切換函式,以及直接改 bg 的 className 換背板。

完整程式碼

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>不規則TAB選項卡效果</title>
<style type="text/css">
body,td{font-size:12px;}
.tab{margin-top:100px}
#TabCon1,#TabCon2{width:212px;height:100px;line-height:100px;
  border-left:#757575 1px solid;border-bottom:#757575 1px solid;border-right:#757575 1px solid;}
.xixi1{width:212px;height:29px;line-height:29px;
  background-image:url(images/news_bghover1.gif);cursor:pointer;}
.xixi2{width:212px;height:29px;line-height:29px;
  background-image:url(images/news_bghover2.gif);cursor:pointer;}
.tab1{width:96px;height:29px;line-height:29px;float:left;text-align:center;
  cursor:pointer;color:#FFF;}
.tab2{width:116px;height:29px;line-height:29px;float:left;text-align:center;
  cursor:pointer;color:#000;}
</style>
</head>
<body style="text-align:center">
<script type="text/javascript">
function setTab03Syn(i){ selectTab03Syn(i); }
function selectTab03Syn(i){
  switch (i) {
    case 1:
      document.getElementById("TabCon1").style.display = "block";
      document.getElementById("TabCon2").style.display = "none";
      document.getElementById("font1").style.color = "#ffffff";
      document.getElementById("font2").style.color = "#000000";
      break;
    case 2:
      document.getElementById("TabCon1").style.display = "none";
      document.getElementById("TabCon2").style.display = "block";
      document.getElementById("font1").style.color = "#000000";
      document.getElementById("font2").style.color = "#ffffff";
      break;
  }
}
</script>
<div class="tab">
 <div id="bg" class="xixi1">
  <div id="font1" class="tab1"
   onMouseOver="setTab03Syn(1);document.getElementById('bg').className='xixi1'">新聞標簽</div>
  <div id="font2" class="tab2"
   onMouseOver="setTab03Syn(2);document.getElementById('bg').className='xixi2'">新聞標簽</div>
 </div>
 <div id="TabCon1">不規則TAB選項卡效果</div>
 <div id="TabCon2" style="display:none">不規則TAB選項卡效果2</div>
</div>
</body>
</html>

用 CSS 3 重寫會更省事

如果不必理會舊版 IE,那兩張背景圖完全可以省下來:給標籤加上 border-radius 就能做出圓角,再用 transition 讓文字顏色與背景色平滑過渡,視覺效果會比換圖更細緻,也少了兩次圖片請求。此外目前只有滑入沒有點擊,觸控裝置上會失效,補一個 onclick 呼叫同一函式即可。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved