使用Javascript实现QQ面板

QQ面板,相关代码网上N多,我把JS简单封装了下,这样以后使用方便些。

简单的说下思路

我写的这个东西一共分为三个类,分别是外面的框架叫做QQPanel,可以点击的部分叫做QQItem,显示链接的部分叫做QQSubItem

显示的时候控制每个Item的Top属性,速度可以调节,其实就是用setTimeout来挪动Item的位置

好了,废话不多说了,看代码吧

 

js部分

  1. var QQPANELIDPREFIX = "QQPanel_";
  2. var QQPANELITEMIDPREFIX = "QQItem_";
  3. var QQPANELSUBITEMIDPREFIX = "QQSubItem_";
  4. var runtimes = 0;
  5. window.QQPanels = [];
  6. function QQPanel()
  7. {
  8.     this.id = window.QQPanels.length;
  9.     window.QQPanels[this.id] = this;
  10.     this.target = "_self";
  11.     this.panelTop = 50;
  12.     this.panelLeft = 50;
  13.     this.panelWidth = 140;
  14.     this.itemHeight = 20;
  15.     this.subItemHeight = 400;
  16.     this.speed = 1;//数字越大,动作越慢
  17.     this.Items = [];
  18. }
  19. function QQItem(text)
  20. {
  21.     this.text = text;
  22.     this.expand = false;
  23.     this.subItems = [];
  24. }
  25. function QQSubItem(text,href)
  26. {
  27.     this.text = text;
  28.     this.href = href;
  29. }
  30. //**********QQPanel prototype**********
  31. QQPanel.prototype.addItem = function(qqitem) 
  32. {
  33.     var index = this.Items.length;
  34.     qqitem.parent = this;
  35.     qqitem.id = QQPANELIDPREFIX + this.id + "_" + QQPANELITEMIDPREFIX + index;
  36.     qqitem.index = index;
  37.     this.Items[index] = qqitem;
  38. }
  39. QQPanel.prototype.getHeight = function()
  40. {
  41.     return this.Items.length * this.itemHeight + this.subItemHeight;
  42. }
  43. QQPanel.prototype.toString = function()
  44. {
  45.     var html = "";
  46.     html += "<span class=panelStyle style=/"left:"+this.panelLeft+"px;top:"+this.panelTop+"px;width:"+this.panelWidth+"px;height:"+this.getHeight()+"px;/">";
  47.     for(var i = 0 ; i < this.Items.length ; i++)
  48.     {
  49.         var top = this.subItemHeight * i;
  50.         html += "<div id="+this.Items[i].id+" style=/"LEFT: 0px; WIDTH: "+this.panelWidth+"px; POSITION: relative; TOP:-"+top+"px/"><TABLE cellSpacing=0 cellPadding=0 width=/"100%/"><TBODY>";
  51.         html += this.Items[i];
  52.         if(this.Items[i].subItems.length > 0)
  53.         {
  54.             html+="<TR><TD class=subItemStyle><div style=/"overflow:auto;height:"+this.subItemHeight+"px;/">";
  55.             for(var q = 0;q<this.Items[i].subItems.length;q++)
  56.             {
  57.                 html += this.Items[i].subItems[q];
  58.             }
  59.             html+="</div></TD></TR>";
  60.         }
  61.         html+="</TBODY></TABLE></DIV>";
  62.     }
  63.     html += "</span>";
  64.     html += "<script>window.QQPanels["+this.id+"].displayQQItem(0)</script>";//默认显示第一个
  65.     return html;
  66. }
  67. QQPanel.prototype.displayQQItem = function(itemIndex)
  68. {
  69.     var offset = parseInt(this.subItemHeight / this.speed)
  70.     var expandItem = this.findExpandQQItem();
  71.     var expandIndex = this.Items.length - 1;
  72.     if (expandItem != null)
  73.     {
  74.         expandIndex = expandItem.index;
  75.     }
  76.     if (itemIndex == expandIndex)
  77.     {
  78.         return;
  79.     }
  80.     
  81.     if(itemIndex - expandIndex > 0)
  82.     {
  83.         offset = (-1) * offset;
  84.         for(i = expandIndex + 1;i <= itemIndex;i++)
  85.         {
  86.             this.Items[i].move(offset);
  87.         }
  88.     }
  89.     else
  90.     { 
  91.         for(i = expandIndex;i > itemIndex;i--)
  92.         {
  93.             this.Items[i].move(offset);
  94.         }
  95.     }
  96.     runtimes++;
  97.     if(runtimes >= this.speed)
  98.     {
  99.         this.Items[itemIndex].expand = true;
  100.         this.Items[expandIndex].expand = false;
  101.         runtimes = 0;
  102.     }
  103.     else
  104.     {
  105.         setTimeout("window.QQPanels["+this.id+"].displayQQItem(itemIndex)",10);
  106.     }
  107. }
  108. QQPanel.prototype.findExpandQQItem = function()
  109. {
  110.     for(var i = 0 ; i < this.Items.length;i++)
  111.     {
  112.         if (this.Items[i].expand)
  113.         {
  114.             return this.Items[i];
  115.         }
  116.     }
  117.     return null;
  118. }
  119. //**********QQPanel prototype**********
  120. //**********QQItem prototype**********
  121. QQItem.prototype.addSubItem = function(qqSubItem)
  122. {
  123.     qqSubItem.parent = this;
  124.     qqSubItem.id = this.id + "_" + QQPANELSUBITEMIDPREFIX + this.subItems.length;
  125.     qqSubItem.index = this.subItems.length;
  126.     this.subItems[this.subItems.length] = qqSubItem;
  127. }
  128. QQItem.prototype.move = function(offset)
  129. {
  130.     var item = document.getElementById(this.id);
  131.     item.style.top = parseInt(item.style.top) + offset;
  132. }
  133. QQItem.prototype.toString = function()
  134. {
  135.     var html = "<tr>";
  136.     html += "<TD class=itemStyle οnclick=window.QQPanels["+this.parent.id+"].displayQQItem("+this.index+") height="+this.parent.itemHeight+"px>";
  137.     html += this.text;
  138.     html += "</TD>";
  139.     html += "</TR>";
  140.     return html;
  141. }
  142. //**********QQItem prototype**********
  143. //**********QQSubItem prototype**********
  144. QQSubItem.prototype.toString = function()
  145. {
  146.     var html = "";
  147.     html += "<BR /><CENTER>";
  148.     var href = "#";
  149.     if (this.href != null && this.href.length > 0)
  150.     {
  151.         href = this.href;
  152.     }
  153.     html += "<a href=/""+href+"/" target=/""+this.parent.parent.target+"/">"+this.text+"</a>";
  154.     html += "</CENTER>";
  155.     return html;
  156. }
  157. //**********QQSubItem prototype**********

css部分

  1. .panelStyle
  2. {
  3.     position:absolute;overflow:hidden;border:1px solid #008800;
  4. }
  5. .itemStyle
  6. {
  7.     background-color:#6699CC;color:#ffffff;
  8.     border-top:1px solid #FFFFFF;font-size:9pt;cursor:pointer;
  9.     text-align:center;
  10. }
  11. .subItemStyle
  12. {
  13.     background-color:#efefef;color:blue;font-size:9pt;
  14. }

使用起来很方便,测试HTML如下

  1. <HEAD>
  2. <link href="QQPanel.css" type="text/css" rel="stylesheet"/>
  3. <script type="text/javascript" src="QQPanel.js"></script>
  4. </HEAD>
  5. <body>
  6. <script language="javascript" type="text/javascript">
  7. var qq = new QQPanel();
  8. for(var i = 0 ; i < 10;i++)
  9. {
  10.     var item = new QQItem("item"+i,null);
  11.     item.addSubItem(new QQSubItem("subitem1"),null);
  12.     item.addSubItem(new QQSubItem("subitem2"),null);
  13.     item.addSubItem(new QQSubItem("subitem3"),null);
  14.     item.addSubItem(new QQSubItem("subitem4"),null);
  15.     qq.addItem(item);
  16. }
  17. document.write(qq);
  18. </script>
  19. </body>

以上代码在IE6+FF3测试通过~~~~

 

欢迎转载,请注明出处~~

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值