QQ面板,相关代码网上N多,我把JS简单封装了下,这样以后使用方便些。
简单的说下思路
我写的这个东西一共分为三个类,分别是外面的框架叫做QQPanel,可以点击的部分叫做QQItem,显示链接的部分叫做QQSubItem
显示的时候控制每个Item的Top属性,速度可以调节,其实就是用setTimeout来挪动Item的位置
好了,废话不多说了,看代码吧
js部分
- var QQPANELIDPREFIX = "QQPanel_";
- var QQPANELITEMIDPREFIX = "QQItem_";
- var QQPANELSUBITEMIDPREFIX = "QQSubItem_";
- var runtimes = 0;
- window.QQPanels = [];
- function QQPanel()
- {
- this.id = window.QQPanels.length;
- window.QQPanels[this.id] = this;
- this.target = "_self";
- this.panelTop = 50;
- this.panelLeft = 50;
- this.panelWidth = 140;
- this.itemHeight = 20;
- this.subItemHeight = 400;
- this.speed = 1;//数字越大,动作越慢
- this.Items = [];
- }
- function QQItem(text)
- {
- this.text = text;
- this.expand = false;
- this.subItems = [];
- }
- function QQSubItem(text,href)
- {
- this.text = text;
- this.href = href;
- }
- //**********QQPanel prototype**********
- QQPanel.prototype.addItem = function(qqitem)
- {
- var index = this.Items.length;
- qqitem.parent = this;
- qqitem.id = QQPANELIDPREFIX + this.id + "_" + QQPANELITEMIDPREFIX + index;
- qqitem.index = index;
- this.Items[index] = qqitem;
- }
- QQPanel.prototype.getHeight = function()
- {
- return this.Items.length * this.itemHeight + this.subItemHeight;
- }
- QQPanel.prototype.toString = function()
- {
- var html = "";
- html += "<span class=panelStyle style=/"left:"+this.panelLeft+"px;top:"+this.panelTop+"px;width:"+this.panelWidth+"px;height:"+this.getHeight()+"px;/">";
- for(var i = 0 ; i < this.Items.length ; i++)
- {
- var top = this.subItemHeight * i;
- 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>";
- html += this.Items[i];
- if(this.Items[i].subItems.length > 0)
- {
- html+="<TR><TD class=subItemStyle><div style=/"overflow:auto;height:"+this.subItemHeight+"px;/">";
- for(var q = 0;q<this.Items[i].subItems.length;q++)
- {
- html += this.Items[i].subItems[q];
- }
- html+="</div></TD></TR>";
- }
- html+="</TBODY></TABLE></DIV>";
- }
- html += "</span>";
- html += "<script>window.QQPanels["+this.id+"].displayQQItem(0)</script>";//默认显示第一个
- return html;
- }
- QQPanel.prototype.displayQQItem = function(itemIndex)
- {
- var offset = parseInt(this.subItemHeight / this.speed)
- var expandItem = this.findExpandQQItem();
- var expandIndex = this.Items.length - 1;
- if (expandItem != null)
- {
- expandIndex = expandItem.index;
- }
- if (itemIndex == expandIndex)
- {
- return;
- }
- if(itemIndex - expandIndex > 0)
- {
- offset = (-1) * offset;
- for(i = expandIndex + 1;i <= itemIndex;i++)
- {
- this.Items[i].move(offset);
- }
- }
- else
- {
- for(i = expandIndex;i > itemIndex;i--)
- {
- this.Items[i].move(offset);
- }
- }
- runtimes++;
- if(runtimes >= this.speed)
- {
- this.Items[itemIndex].expand = true;
- this.Items[expandIndex].expand = false;
- runtimes = 0;
- }
- else
- {
- setTimeout("window.QQPanels["+this.id+"].displayQQItem(itemIndex)",10);
- }
- }
- QQPanel.prototype.findExpandQQItem = function()
- {
- for(var i = 0 ; i < this.Items.length;i++)
- {
- if (this.Items[i].expand)
- {
- return this.Items[i];
- }
- }
- return null;
- }
- //**********QQPanel prototype**********
- //**********QQItem prototype**********
- QQItem.prototype.addSubItem = function(qqSubItem)
- {
- qqSubItem.parent = this;
- qqSubItem.id = this.id + "_" + QQPANELSUBITEMIDPREFIX + this.subItems.length;
- qqSubItem.index = this.subItems.length;
- this.subItems[this.subItems.length] = qqSubItem;
- }
- QQItem.prototype.move = function(offset)
- {
- var item = document.getElementById(this.id);
- item.style.top = parseInt(item.style.top) + offset;
- }
- QQItem.prototype.toString = function()
- {
- var html = "<tr>";
- html += "<TD class=itemStyle οnclick=window.QQPanels["+this.parent.id+"].displayQQItem("+this.index+") height="+this.parent.itemHeight+"px>";
- html += this.text;
- html += "</TD>";
- html += "</TR>";
- return html;
- }
- //**********QQItem prototype**********
- //**********QQSubItem prototype**********
- QQSubItem.prototype.toString = function()
- {
- var html = "";
- html += "<BR /><CENTER>";
- var href = "#";
- if (this.href != null && this.href.length > 0)
- {
- href = this.href;
- }
- html += "<a href=/""+href+"/" target=/""+this.parent.parent.target+"/">"+this.text+"</a>";
- html += "</CENTER>";
- return html;
- }
- //**********QQSubItem prototype**********
css部分
- .panelStyle
- {
- position:absolute;overflow:hidden;border:1px solid #008800;
- }
- .itemStyle
- {
- background-color:#6699CC;color:#ffffff;
- border-top:1px solid #FFFFFF;font-size:9pt;cursor:pointer;
- text-align:center;
- }
- .subItemStyle
- {
- background-color:#efefef;color:blue;font-size:9pt;
- }
使用起来很方便,测试HTML如下
- <HEAD>
- <link href="QQPanel.css" type="text/css" rel="stylesheet"/>
- <script type="text/javascript" src="QQPanel.js"></script>
- </HEAD>
- <body>
- <script language="javascript" type="text/javascript">
- var qq = new QQPanel();
- for(var i = 0 ; i < 10;i++)
- {
- var item = new QQItem("item"+i,null);
- item.addSubItem(new QQSubItem("subitem1"),null);
- item.addSubItem(new QQSubItem("subitem2"),null);
- item.addSubItem(new QQSubItem("subitem3"),null);
- item.addSubItem(new QQSubItem("subitem4"),null);
- qq.addItem(item);
- }
- document.write(qq);
- </script>
- </body>
以上代码在IE6+FF3测试通过~~~~
欢迎转载,请注明出处~~