萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS3 仿 Windows 7 開始選單完整實作教學

專案預覽

純 CSS3 實現 Windows 7 風格開始選單,包含圓角、陰影、漸層、動態懸停效果,無需 JavaScript 與圖片(圖示除外)。

HTML 結構

選單分為左側程式清單(#programs)與右側系統連結(#links):

<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>

核心 CSS 技術解析

1. 選單容器 #startmenu

#startmenu {
  border: solid 1px #102a3e;
  overflow: visible;
  display: inline-block;
  margin: 60px 0 0 20px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  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));
  background: linear-gradient(to bottom, rgba(50,123,165,0.75), rgba(46,75,90,0.75) 50%, rgba(92,176,220,0.75));
}

關鍵點:

  • border-radius:圓角效果(CSS3 最廣泛應用)
  • box-shadow: inset ...:內陰影營造凹陷質感
  • linear-gradient:多色停漸層背景,模擬 Win7 玻璃質感

2. 左側程式清單 #programs

#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;
  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;
  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));
  background: linear-gradient(to bottom, #dcebfd, #c2dcfd);
}
#programs a img { border: 0; vertical-align: middle; margin: 0 5px 0 0; }

重點: 懸停狀態結合圓角、內陰影、漸層,完美重現 Win7 選單項目高亮效果。

3. 右側系統連結 #links

#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;
  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));
  background: linear-gradient(to right, rgba(81,115,132,0.55), rgba(121,163,184,0.55) 50%, rgba(81,115,132,0.55));
}
#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)));
}

瀏覽器相容性說明

  • 完整支援:Firefox 3.6+、Chrome、Safari、Opera 10.5+、Edge、IE10+
  • IE9 僅支援部份漸層語法(需 -ms-filter),圓角與陰影正常
  • IE8 及以下不支援 CSS3 漸層、圓角、陰影,需搭配 PIE.htc 或降級處理

延伸應用

此技術可擴展應用於:下拉選單、工具提示、側邊導航、模擬桌面 UI 介面等。善用 CSS3 視覺屬性,能大幅減少圖片請求與 JavaScript 依賴。

copyright © 萬盛學電腦網 all rights reserved