javascript仿qq介面的摺疊菜單實現代碼

來源:互聯網
上載者:User

最近一直在研究網頁特效,看到qq介面的摺疊菜單,於是冒出個想法,自己寫一個類似的,上網查了一下,發現已經有不少類似的菜單效果,不管那麼多,先寫著再說吧.

以下是html結構:
<div id="a"><div id="h1">sdfds</div><div id="b1">dsfdsfsdfsd</div><div id="h2">dsf</div><div id="b2">dsfdsfsdf</div>.......................</div>
id為a裡的便是你要添加的菜單的標題和內容.

下面是javascript代碼: 複製代碼 代碼如下:sx.activex.packmenu={
create:function(t){
var a=document.createElement("div");
var _t=t;
a.style.height="300px";
a.style.width="150px";
a.style.border="1px red solid";
a.style.overflow="hidden";
for(var i=0;i<t.length;i++){
var h=document.createElement("div");
var b=document.createElement("div");;
h.style.backgroundColor="blue";
h.style.height="10%";
b.style.padding="5px";
b.style.textAlign="center";
b.style.border="1px green solid";
h.innerHTML=t[i][0];
b.innerHTML=t[i][1];
b.style.overflow="hidden";
b.style.height="0px";
b.style.display="none";
h.onclick=function(){
if(this.nextSibling.style.display=="none"){
this.nextSibling.style.height="1px";
this.nextSibling.style.display="block";
this.h=window.setInterval(function(t,t1){
return function(){
if(!t1) return;
//alert(t.nextSibling.offsetHeight);
if(parseInt(t.nextSibling.style.height)<100-_t.length*parseInt(a.all[0].style.height)-3){
t.nextSibling.style.height=parseInt(t.nextSibling.style.height)+3+"%";
t1.style.height=parseInt(t1.style.height)-3+"%";
}
else{
t.nextSibling.style.height=100-_t.length*parseInt(a.all[0].style.height)+"%";;
t1.style.display="none";
t1.style.height="0px";
window.clearInterval(t.h);
}
}
}(this,(function(){
for(var ii=0;ii<a.all.length;ii++){
if(parseInt(a.all[ii].style.height)>10)
return a.all[ii];
}
})()),10);
}
else{
if(this!=this.parentNode.firstChild){
this.previousSibling.style.height="1px";
this.previousSibling.style.display="block";
}
else{
this.parentNode.lastChild.style.display="block";
this.parentNode.lastChild.style.height="1px";
}
this.h=window.setInterval(function(t,t1){
return function(){
if(!t1) return;
if(parseInt(t.nextSibling.style.height)>3){
if(t!=t.parentNode.firstChild)
t.previousSibling.style.height=parseInt(t.previousSibling.style.height)+3+"%";
else
t.parentNode.lastChild.style.height=parseInt(t.parentNode.lastChild.style.height)+3+"%";
t1.style.height=parseInt(t1.style.height)-3+"%";

}
else{
if(t!=t.parentNode.firstChild)
t.previousSibling.style.height=100-_t.length*parseInt(a.all[0].style.height)+"%";
else
t.parentNode.lastChild.style.height=100-_t.length*parseInt(a.all[0].style.height)+"%";
t.nextSibling.style.display="none";
t.nextSibling.style.height="0px";
window.clearInterval(t.h);
}
}
}(this,(function(){
for(var ii=0;ii<a.all.length;ii++){
if(parseInt(a.all[ii].style.height)>10)
return a.all[ii];
}
})()),1);
}
}
a.appendChild(h);
a.appendChild(b);
a.all[1].style.display="block";
a.all[1].style.height=100-t.length*parseInt(a.all[0].style.height)+"%";
}
return a;
}
}

入口參數t是你要傳遞的二維數組,以一個標題加內容為一個數組項的形式:
下面是調用代碼:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script src="kongjian.js" ></script>
<script>
var a=sx.activex.packmenu.create([["asd","sadsadas"],["sd","sadsaas"],["w","waedqwdq"],["e","efwefw"]]);
//a.contentEditable=true;
document.body.appendChild(a);
</script>
</body>
</html>
搞定,有興趣的朋友可以看下效果.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.