DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
用 CSS3 做一個 Windows 7 風格開始菜單
編輯:布局實例     

圓角、內陰影、漸層背景——Windows 7 開始菜單的質感,幾乎全靠這三樣撐起來。而這三樣正好是 CSS3 最成熟的部分,不需要任何圖片就能重現。

第一步:建立結構

菜單分兩欄:左側是程式列表,右側是系統位置捷徑。

<div id="startmenu">
  <ul id="programs">
    <li><a href="#"><img src="firefox-32.png" alt="" />Mozilla Firefox</a></li>
    <li><a href="#"><img src="chrome.png" alt="" />Google Chrome</a></li>
    <li><a href="#"><img src="safari.png" alt="" />Safari</a></li>
    <li><a href="#"><img src="opera.png" alt="" />Opera</a></li>
    <li><a href="#"><img src="ie.png" alt="" />Internet Explorer</a></li>
    <li><a href="#"><img src="rss_32.png" alt="" />RSS Feeds</a></li>
    <li><a href="#"><img src="twitter_32.png" alt="" />Twitter</a></li>
    <li><a href="#"><img src="delicious_32.png" alt="" />Delicious</a></li>
  </ul>
  <ul id="links">
    <li class="icon"><img src="folder.png" alt="" /></li>
    <li><a href="#"><span>Documents</span></a></li>
    <li><a href="#"><span>Pictures</span></a></li>
    <li><a href="#"><span>Music</span></a></li>
    <li><a href="#"><span>Computer</span></a></li>
    <li><a href="#"><span>Network</span></a></li>
    <li><a href="#"><span>Connect to</span></a></li>
  </ul>
</div>

第二步:容器外觀

#startmenu {
  border: solid 1px #102a3e;
  overflow: visible;
  display: inline-block;
  margin: 60px 0 0 20px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  position: relative;
  box-shadow: inset 0 0 1px #fff;
  -moz-box-shadow: inset 0 0 1px #fff;
  -webkit-box-shadow: inset 0 0 1px #fff;
  background-color: #619bb9;
  background: -moz-linear-gradient(top, rgba(50,123,165,0.75), rgba(46,75,90,0.75) 50%, rgba(92,176,220,0.75));
  background: -webkit-gradient(linear, center top, center bottom, from(#327aa4), color-stop(45%, #2e4b5a), to(#5cb0dc));
}

三個值得注意的點:

  • border-radius 做出圓角,這是 CSS3 落地最成功的功能之一。
  • box-shadow 的 inset 讓陰影往內投射,模擬玻璃面板的內緣高光。
  • linear-gradient 三段色停靠(亮藍 → 深藍綠 → 亮藍)重現 Win7 那種帶金屬感的漸層。

注意每個屬性都寫了 -moz- 與 -webkit- 前綴版本,這是當時瀏覽器尚未定案的過渡寫法;今天標準屬性已普遍支援,前綴可以省略。

第三步:左側程式列表

#programs {
  background: #fff;
  border: solid 1px #365167;
  margin: 7px 0 7px 7px;
  box-shadow: 0 0 1px #fff;
  -moz-box-shadow: 0 0 1px #fff;
  -webkit-box-shadow: 0 0 1px #fff;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
}
#programs a {
  border: solid 1px transparent;
  display: block;
  padding: 3px;
  margin: 3px;
  color: #4b4b4b;
  text-decoration: none;
  min-width: 220px;
}
#programs a:hover {
  border: solid 1px #7da2ce;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  box-shadow: inset 0 0 1px #fff;
  -moz-box-shadow: inset 0 0 1px #fff;
  -webkit-box-shadow: inset 0 0 1px #fff;
  background-color: #cfe3fd;
  background: -moz-linear-gradient(top, #dcebfd, #c2dcfd);
  background: -webkit-gradient(linear, center top, center bottom, from(#dcebfd), to(#c2dcfd));
}
#programs a img { border: 0; vertical-align: middle; margin: 0 5px 0 0; }

這裡的巧思在於 a 預設就有一個 透明 1px 邊框。滑過時只改顏色不改寬度,因此項目不會位移——這是 hover 效果最常見的排版 bug 來源,先佔位就能徹底避免。

第四步:右側系統捷徑

#links { margin: 7px; margin-top: -30px; }
#links li.icon { text-align: center; }
#links a {
  border: solid 1px transparent;
  display: block;
  margin: 5px 0;
  position: relative;
  color: #fff;
  text-decoration: none;
  min-width: 120px;
}
#links a:hover {
  border: solid 1px #000;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  box-shadow: 0 0 1px #fff;
  -moz-box-shadow: inset 0 0 1px #fff;
  -webkit-box-shadow: inset 0 0 1px #fff;
  background-color: #658da0;
  background: -moz-linear-gradient(center left, rgba(81,115,132,0.55), rgba(121,163,184,0.55) 50%, rgba(81,115,132,0.55));
  background: -webkit-gradient(linear, 0% 100%, 100% 100%, from(#517384), color-stop(50%, #79a3b8), to(#517384));
}
#links a span { padding: 5px; display: block; }
#links a:hover span {
  background: -moz-linear-gradient(center top, transparent, transparent 49%, rgba(2,37,58,0.5) 50%, rgba(63,111,135,0.5));
  background: -webkit-gradient(linear, center top, center bottom, from(transparent), color-stop(49%, transparent), color-stop(50%, rgba(2,37,58,0.5)), to(rgba(63,111,135,0.5)));
}

右欄的 margin-top:-30px 是為了讓系統圖示區往上提,與左欄的頂部對齊。而 hover 時 span 的漸層刻意在中線做一個 49% 到 50% 的硬轉折,模擬 Win7 項目反白時那道細緻的分界線。

寫完之後

這個範例真正的難點不在語法,而在細節的螺絲起子——漸層的色停靠位置、透明邊框的預留、負邊距的對齊。CSS3 的漸層非常靈活,但也因此調一個 1% 就會看出差別。若要做更進一步的質感(玻璃模糊、深層陰影),今天的 backdrop-filter 與多層 box-shadow 能省下大量切圖功夫。

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