找回密码
 注册
【阿里云】2核2G云服务器新老同享 99元/年,续费同价,云服务器3年机/5年机限时抢购,低至 2.5折华为云精选云产品特惠糖果主机Jtti,新加坡服务器,美国服务器,香港服务器,海外云服务器
查看: 999|回复: 1

有趣的仿QQ面板导航菜单

[复制链接]
发表于 2005 年 6 月 16 日 11:18:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
  QQ的组切换效果相信大家都很熟悉,现在我们在网页中用Javascript模拟制作一个这样的切换效果,用它作为页面导航菜单可是个不错的主意哦!

  以下是效果演示,其中菜单的组数和内容可以任意定义。

  代码及其解释:
   先创建一个CSS样式表,置于<HEAD>模块中,分别定义导航菜单的标题和内容的样式:
<style type="text/css">
  .titleStyle{
     background-color:#008800;color:#ffffff;
     border-top:1px solid #FFFFFF;font-size:9pt;cursor:hand;
  }
  .contentStyle{
     background-color:#eeffee;color:blue;font-size:9pt;
  }
</style>
   然后在<BODY>模块中加入以下Javascript代码:

<script language="JavaScript">
<!--
var layerTop=20;       //菜单顶边距
var layerLeft=30;      //菜单左边距
var layerWidth=140;    //菜单总宽度
var titleHeight=20;    //标题栏高度
var contentHeight=200; //内容区高度
var stepNo=10;         //移动步数,数值越大移动越慢
var itemNo=0;
  //建立一个名为"itemsLayer"的层,用它限制所有菜单的显示范围:
document.write('<span id=itemsLayer
style="position:absolute;overflow:hidden;border:1px
solid #008800;left:'+layerLeft+';top:'+
layerTop+';width:'+layerWidth+';">');
function addItem(itemTitle,itemContent){
  //这个函数准备接受菜单标题和内容的写入
    //新增菜单的顶点刚好在上一菜单的标题栏以下,尺寸由一开始便声明的变量决定
  itemHTML='<div id=item'+itemNo+' itemIndex='+itemNo+
' style="position:relative;left:0;
top:'+(-contentHeight*itemNo)+';width:'+layerWidth+';">'+
      '<table width=100% cellspacing="0" cellpadding="0">'+
      '<tr><td height='+titleHeight+
' onclick=changeItem('+itemNo+') class="titleStyle"
align=center>'+itemTitle+'</td></tr>'+
      '<tr><td height='+contentHeight+' class="contentStyle">'
+itemContent+'</td></tr> </table></div>';
  document.write(itemHTML);
  itemNo++;
}
//这时便可以把菜单标题和内容作为参数调用以上函数了:
  //你可以添加任意多项,格式参照以下几行:

addItem('欢迎','<BR>欢迎光临设计在线!');
addItem('网页陶吧','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a>
<BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
addItem('美工教室','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a>
<BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
addItem('Flash','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a>
<BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
addItem('多媒体','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a>
<BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
addItem('精品赏析','<a href="#">设计精品</a>');
document.write('</span>');   //结束"itemsLayer"层

  //下面一式计算"itemsLayer"层的高度:
document.all.itemsLayer.style.height =
itemNo*titleHeight+contentHeight;
//现在开始编写点击标题时移动相应的层:
  //初始化变量"toItemIndex"和"onItemIndex",
它们分别用于记录"应该显示的层"和"现在显示的层":
var toItemIndex=itemNo-1;
var onItemIndex=itemNo-1;
var runtimes=0;  //"runtimes"用于记录层移动次数
  //菜单标题被点击时调用这个函数:
function changeItem(clickItemIndex){
   //判断相应的层应上移还是下移:
  toItemIndex=clickItemIndex;
  if(toItemIndex-onItemIndex>0) moveUp(); else moveDown();
   //一定的时间间隔后继续移动,直到移了设定的步数stepNo:
  runtimes++;
  if(runtimes>=stepNo){
    onItemIndex=toItemIndex;
    runtimes=0;}
  else
    setTimeout("changeItem(toItemIndex)",10);
}
  //相应菜单上移:
function moveUp(){
   //判断应一起上移的菜单,并让它(们)每次移动contentHeight/stepNo的距离:
  for(i=onItemIndex+1;i<=toItemIndex;i++)
    eval('document.all.item'+i+'.style.top=
parseInt(document.all.item'+i+'.style.top)-contentHeight/stepNo;');
}
  //相应菜单下移:
function moveDown(){
  for(i=onItemIndex;i>toItemIndex;i--)
    eval('document.all.item'+i+'.style.top=
parseInt(document.all.item'+i+'.style.top)+contentHeight/stepNo;');
}
changeItem(0); //把第一个菜单作为默认显示
//-->
</script>
(该脚本在windows 2000、windows XP及各自带的IE下测试通过)

完整代码:
  1. <html>
  2. <head>
  3. <title>TEST</title>
  4. <style type="text/css">
  5. .titleStyle{
  6.    background-color:#008800;color:#ffffff;
  7.    border-top:1px solid #FFFFFF;font-size:9pt;cursor:hand;
  8. }
  9. .contentStyle{
  10.    background-color:#eeffee;color:blue;font-size:9pt;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script language="JavaScript">
  16. <!--
  17. var layerTop=20;     //菜单顶边距
  18. var layerLeft=30;     //菜单左边距
  19. var layerWidth=140;   //菜单总宽度
  20. var titleHeight=20;   //标题栏高度
  21. var contentHeight=200; //内容区高度
  22. var stepNo=1;       //移动步数,数值越大移动越慢
  23. var itemNo=0;
  24. //建立一个名为"itemsLayer"的层,用它限制所有菜单的显示范围:
  25. document.write('<span id=itemsLayer style="position:absolute;overflow:hidden;border:1px solid #008800;left:'+layerLeft+';top:'+layerTop+';width:'+layerWidth+';">');
  26. function addItem(itemTitle,itemContent){
  27.   //这个函数准备接受菜单标题和内容的写入
  28.   //新增菜单的顶点刚好在上一菜单的标题栏以下,尺寸由一开始便声明的变量决定
  29.   itemHTML='<div id=item'+itemNo+' itemIndex='+itemNo+' style="position:relative;left:0;top:'+(-contentHeight*itemNo)+';width:'+layerWidth+';">'+'<table width=100% cellspacing="0" cellpadding="0">'+'<tr><td height='+titleHeight+' onclick=changeItem('+itemNo+') class="titleStyle" align=center>'+itemTitle+'</td></tr>'+'<tr><td height='+contentHeight+' class="contentStyle">'+itemContent+'</td></tr> </table></div>';
  30.   document.write(itemHTML);
  31.   itemNo++;
  32. }
  33. //这时便可以把菜单标题和内容作为参数调用以上函数了:
  34. //你可以添加任意多项,格式参照以下几行:
  35. addItem('欢迎','<BR>欢迎光临设计在线!');
  36. addItem('网页陶吧','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a><BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
  37. addItem('美工教室','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a><BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
  38. addItem('Flash','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a><BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
  39. addItem('多媒体','<center><a href="#">项目</a> <BR><BR><a href="#">项目</a><BR><BR><a href="#">项目</a> <BR><BR><a href="#">更多..</a></center>');
  40. addItem('精品赏析','<a href="#">设计精品</a>');
  41. document.write('</span>');   //结束"itemsLayer"层
  42. //下面一式计算"itemsLayer"层的高度:
  43. document.all.itemsLayer.style.height = itemNo*titleHeight+contentHeight;
  44. //现在开始编写点击标题时移动相应的层:
  45. //初始化变量"toItemIndex"和"onItemIndex",它们分别用于记录"应该显示的层"和"现在显示的层":
  46. var toItemIndex=itemNo-1;
  47. var onItemIndex=itemNo-1;
  48. var runtimes=0; //"runtimes"用于记录层移动次数
  49. //菜单标题被点击时调用这个函数:
  50. function changeItem(clickItemIndex){
  51.   //判断相应的层应上移还是下移:
  52.   toItemIndex=clickItemIndex;
  53.   if(toItemIndex-onItemIndex>0) moveUp(); else moveDown();
  54.   //一定的时间间隔后继续移动,直到移了设定的步数stepNo:
  55.   runtimes++;
  56.   if(runtimes>=stepNo){
  57.   onItemIndex=toItemIndex;
  58.   runtimes=0;}
  59.   else
  60.   setTimeout("changeItem(toItemIndex)",10);
  61. }
  62. //相应菜单上移:
  63. function moveUp(){
  64.   //判断应一起上移的菜单,并让它(们)每次移动contentHeight/stepNo的距离:
  65.   for(i=onItemIndex+1;i<=toItemIndex;i++)
  66.   eval('document.all.item'+i+'.style.top=parseInt(document.all.item'+i+'.style.top)-contentHeight/stepNo;');
  67. }
  68. //相应菜单下移:
  69. function moveDown(){
  70.   for(i=onItemIndex;i>toItemIndex;i--)
  71.   eval('document.all.item'+i+'.style.top=parseInt(document.all.item'+i+'.style.top)+contentHeight/stepNo;');
  72. }
  73. changeItem(0); //把第一个菜单作为默认显示
  74. //-->
  75. </script>
  76. </body>
  77. </html>
复制代码

以后发代码注意排版,原来帖出的代码复制后不能直接使用。
Jgwy.Com - Free Web Hosting Guide & Directory In China since 2001! Jgwy.Net-Jglt.Net
发表于 2005 年 6 月 16 日 12:33:34 | 显示全部楼层
【腾讯云】2核2G云服务器新老同享 99元/年,续费同价
Firefox1.0.4可用,但代码不规范。
Jgwy.Com - Free Web Hosting Guide & Directory In China since 2001! Jgwy.Net-Jglt.Net
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|金光论坛

GMT+8, 2024 年 9 月 20 日 21:23 , Processed in 0.167099 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表