DIV CSS 佈局教程網

用原生 JavaScript 打造仿雅虎首頁風格的快捷登入與導覽模組
編輯:關於JavaScript     

本文分享一個以原生 JavaScript 完成的仿雅虎首頁元件:外觀是一塊「便捷入口」面板,裡頭同時藏著快速登入表單與可切換的導覽清單。整段程式不依賴任何框架,直接放到網站首頁的側欄,就能當成會員登入區來用。

這個元件做了什麼

面板標題列上有一顆「快速登陸」連結,點下去會把原本的第一個清單項目收起來,改為展開帳號欄、密碼欄與「立即登陸」按鈕;滑鼠移到下方任一個入口標籤時,對應的次要連結會跟著浮現,該標籤本身也切換成高亮狀態。所有互動都是靠原生 DOM 操作加上 className 切換完成,沒有寫一行 jQuery。

執行結果如下:

線上展示:https://demo.jb51.net/js/2015/js-f-yahoo-index-login-style-codes/

版面與樣式的關鍵

面板寬度固定 224px,背景是一張 box.png;每個入口標籤都從同一張 title.gif 精靈圖裁切出來,差別只在 background-position 的位移數值。滑鼠移入時換成另一組座標,就能做出「按下去有厚度」的視覺回饋。列表本身用 float 排成兩欄,再用 clearfix 收尾避免高度塌陷。

互動邏輯拆解

腳本一開始先把 login 容器內的 h2、dl、ul、li、span、div 全部抓成集合,之後所有切換動作都只是反覆做三件事:清掉全部 span 的 id、清掉全部 div 的 className、再把目標元素設定成 block。id 之所以被拿來當狀態標記,是因為樣式表裡每個高亮狀態都寫成了 #yhzc、#hysj 這類規則,這算是很早期的寫法,維護時不若直接操作 class 直觀。

接著用一個 for 迴圈把 show() 跑三次,為 btype1~btype3 各綁上 onmouseover。type() 裡透過 this.num 記住目前是哪個 span,就能精準決定要顯示哪一塊 div。

完整程式碼

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>仿雅虎首頁網站快捷入口和快速登陸</title>
<style>
*{ margin:0; padding:0; border:none;}
body{font:12px/130% verdana,geneva,arial,helvetica,sans-serif; height:100%;}
li{list-style:none;}
html{ height:100%;}
.clearfix:after{content:" ";display:block;height:0;clear:both;visibility:hidden;}
.clearfix{display:inline-block;}
a:link,a:visited,a:hover{ color:#000; text-decoration:none; }
.rrow{width:224px;}
.login{ background:url(images/box.png); height:210px;width:224px; overflow:hidden;}
.login h2{ color:#fff; font-size:14px; padding:4px 12px; line-height:30px;height:24px; overflow:hidden; }
.login h2 span{ font-size:12px; float:right;margin:-31px 23px 0 0;line-height:32px;font-weight:400; cursor:pointer;}
.rrow ul{ margin:5px 6px; }
.rrow ul li {width:216px; height:31px;margin-bottom:5px; overflow:hidden; float:left;}
.rrow ul li a,.rrow ul li span{ cursor:pointer; display:block; float:left; margin-right:4px;
  background:url(images/title.gif) 0 -152px; width:69px;padding-left:35px; height:31px;
  overflow:hidden; line-height:32px; font-weight:700;}
.rrow ul li .lhover{ background:url(images/title.gif) 0 -183px; height:37px;}
.rrow ul li .yhzc{ background:url(images/title.gif) 0 -152px;}
#yhzc{background:url(images/title.gif) 0 -183px;height:37px;}
.rrow ul li .hysj{ background:url(images/title.gif) -104px -152px; }
#hysj{ background:url(images/title.gif) -104px -183px;height:37px;}
.rrow ul li .fbxx{ background:url(images/title.gif) 0 -220px; }
#fbxx{ background:url(images/title.gif) 0 -251px;height:37px;}
.rrow ul li .gstg{ background:url(images/title.gif) -104px -220px; }
#gstg{ background:url(images/title.gif) -104px -251px;height:37px;}
.rrow ul li .xzmj{ background:url(images/title.gif) 0 -288px; }
#xzmj{ background:url(images/title.gif) 0 -319px;height:37px;}
.rrow ul li .bzzx{ background:url(images/title.gif) -104px -288px; }
#bzzx{ background:url(images/title.gif) -104px -319px;height:37px;}
.rrow ul .hoverli{background:url(images/title.gif) 0 -357px;height:92px; overflow:hidden;
  line-height:32px; font-weight:700;}
.qlogin{height:92px; width:210px; line-height:32px; margin:5px 0 0 10px; display:none;}
.qlogin dt{ height:0; overflow:hidden; }
.qlogin dd{ padding:4px; width:210px; height:22px; text-align:left; float:left;clear:both;overflow:hidden;}
.qlogin dd .passw{ margin:-4px 12px 0 0; float:right;}
.rrow ul li div{ margin-top:5px; width:210px; display:none;}
.rrow ul li div a{background:url(images/title.gif) -80px -1px; height:20px; line-height:20px;
  clear:both; font-weight:400; width:170px;}
.rrow ul li div a:hover{ text-decoration:underline; }
.rrow ul li .block,.rrow .block{ display:block; }
.rrow ul .none{ display:none; }
.btn{font-size:12px; font-family:宋體; float:left; width:57px; height:20px; overflow:hidden;
  background:url(images/title.gif); line-height:21px;padding-left:18px;margin-right:10px; }
a:hover.btn{background:url(images/title.gif) 0 -20px;}
#btn4{background:url(images/title.gif);width:75px; margin-left:48px; cursor:pointer;}
.input2{font-size:12px; border:1px solid #999; width:140px; height:16px; padding:1px 2px 0 2px; }
</style>
</head>
<body>
<table width="18%" height="420" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td>
<div class="rrow">
 <div id="login" class="login">
  <h2>便捷入口<span><a class="w" title="快速登陸">快速登陸</a></span></h2>
  <dl class="qlogin clearfix">
   <dt>快速登陸</dt>
   <dd>用戶名:<input name="text" type="text" class="input2" /></dd>
   <dd>密   碼:<input name="password" type="password" class="input2" /></dd>
   <dd><input name="submit" type="submit" class="btn" id="btn4" value="立即登陸" />
    <a class="passw" href="#">忘記密碼?</a></dd>
  </dl>
  <ul>
   <li id="btype1" class="hoverli">
    <span class="yhzc" id="yhzc" title="用戶注冊">用戶注冊</span>
    <span class="hysj" title="會員升級">會員升級</span>
    <div class="block"><a href="#">1...</a><a href="#">1...</a></div>
    <div><a href="#">2...</a><a href="#">2...</a></div>
   </li>
   <li id="btype2">
    <span class="fbxx" title="發布信息">發布信息</span>
    <span class="gstg" title="公司推廣">公司推廣</span>
    <div><a href="#">3...</a><a href="#">3...</a></div>
   </li>
   <li id="btype3">
    <span class="xzmj" title="尋找買家">尋找買家</span>
    <span class="bzzx" title="幫助中心">幫助中心</span>
   </li>
  </ul>
 </div>
<script type="text/javascript">
var login = document.getElementById("login");
var yhzc = document.getElementById("yhzc");
var h2 = login.getElementsByTagName("h2");
var dl = login.getElementsByTagName("dl");
var ul = login.getElementsByTagName("ul");
var lis = ul[0].getElementsByTagName("li");
var allspan = ul[0].getElementsByTagName("span");
var divs = login.getElementsByTagName("div");

// 點擊「快速登陸」:清空所有狀態後展開登入表單
document.getElementsByTagName("h2")[0].getElementsByTagName("a")[0].onclick = function () {
  for (var i = 0; i < divs.length; i++) { divs[i].className = null; }
  for (var i = 0; i < allspan.length; i++) { allspan[i].id = null; }
  lis[0].className = "none";
  dl[0].className = "qlogin block";
};

for (var x = 1; x < 4; x++) { show(x); }

function show(x) {
  var btype = document.getElementById("btype" + x);
  var bdivs = btype.getElementsByTagName("div");
  var spans = btype.getElementsByTagName("span");
  for (var i = 0; i < spans.length; i++) {
    spans[i].num = i;
    spans[i].onmouseover = type;
  }
  function type() {
    for (var i = 0; i < lis.length; i++) { lis[i].className = null; }
    for (var i = 0; i < allspan.length; i++) { allspan[i].id = null; }
    for (var i = 0; i < divs.length; i++) { divs[i].className = null; }
    dl[0].className = "qlogin";
    yhzc.id = null;
    spans[this.num].id = spans[this.num].className;
    bdivs[this.num].className = "block";
    btype.className = "hoverli";
  }
}
</script>
</div>
</td></tr></table>
</body>
</html> 

可以怎麼改

如果只是要拿來當自家首頁的登入框,把 dl 的預設 className 直接寫成 "qlogin block",再把清單的第一項移除即可;想改成點擊展開而非滑入展開,把 onmouseover 換成 onclick 並補上反向切換的判斷就很夠用了。

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