javascript仿qq界面的折叠菜单实现代码


Posted in Javascript onDecember 12, 2012

最近一直在研究网页特效,看到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>
搞定,有兴趣的朋友可以看下效果.
Javascript 相关文章推荐
jQuery 性能优化指南(2)
May 21 Javascript
Javascript 日期处理之时区问题
Oct 08 Javascript
获取offsetTop和offsetLeft值的js代码(兼容)
Apr 16 Javascript
jQuery实现列表的全选功能
Mar 18 Javascript
JavaScript自定义函数实现查找两个字符串最长公共子串的方法
Nov 24 Javascript
妙用Angularjs实现表格按指定列排序
Jun 23 Javascript
Vue学习笔记进阶篇之vue-router安装及使用方法
Jul 19 Javascript
利用yarn代替npm管理前端项目模块依赖的方法详解
Sep 04 Javascript
jsonp跨域获取数据的基础教程
Jul 01 Javascript
更改BootStrap popover的默认样式及popover简单用法
Sep 13 Javascript
深入理解JavaScript 箭头函数
May 30 Javascript
vue实现微信浏览器左上角返回按钮拦截功能
Jan 18 Javascript
日历查询的算法 如何计算某一天是星期几
Dec 12 #Javascript
javascript实现日历控件(年月日关闭按钮)
Dec 12 #Javascript
关于js中alert弹出窗口文本换行问题简单详细说明
Dec 11 #Javascript
用js判断页面是否加载完成实现代码
Dec 11 #Javascript
ajax页面无刷新 IE下遭遇Ajax缓存导致数据不更新的问题
Dec 11 #Javascript
IE6浏览器下resize事件被执行了多次解决方法
Dec 11 #Javascript
什么是json和jsonp,jQuery json实例详详细说明
Dec 11 #Javascript
You might like
967 个函式
2006/10/09 PHP
PHP中将字符串转化为整数(int) intval() printf() 性能测试
2020/03/20 PHP
ajax返回值中有回车换行、空格的解决方法分享
2013/10/24 PHP
php smarty模板引擎的6个小技巧
2014/04/24 PHP
php中heredoc与nowdoc介绍
2014/12/25 PHP
PHP文件缓存类示例分享
2015/01/30 PHP
如何在Laravel5.8中正确地应用Repository设计模式
2019/11/26 PHP
jQuery源码分析-02正则表达式 RegExp 常用正则表达式
2011/11/14 Javascript
Jquery弹出窗口插件 LeanModal的使用方法
2012/03/10 Javascript
深入理解JavaScript编程中的原型概念
2015/06/25 Javascript
原生JavaScript编写canvas版的连连看游戏
2016/05/29 Javascript
Extjs 点击复选框在表格中增加相关信息行
2016/07/12 Javascript
微信小程序左右滑动切换页面详解及实例代码
2017/02/28 Javascript
基于Vue2实现的仿手机QQ单页面应用功能(接入聊天机器人 )
2017/03/30 Javascript
在Vue中如何使用Cookie操作实例
2017/07/27 Javascript
AngularJS实现的生成随机数与猜数字大小功能示例
2017/12/25 Javascript
webpack 打包压缩js和css的方法示例
2018/03/20 Javascript
vue 使用外部JS与调用原生API操作示例
2019/12/02 Javascript
JavaScript 中的六种循环方法
2021/01/06 Javascript
[03:01]完美盛典趣味短片 DOTA2年度最佳&拉胯英雄
2019/12/07 DOTA
Python set集合类型操作总结
2014/11/07 Python
python3.6使用urllib完成下载的实例
2018/12/19 Python
Django 项目通过加载不同env文件来区分不同环境
2020/02/17 Python
html5弹跳球示例代码
2013/07/23 HTML / CSS
JD Sports芬兰:英国领先的运动鞋和运动服饰零售商
2018/11/16 全球购物
阿里巴巴英国:Alibaba英国
2019/12/11 全球购物
路政管理专业推荐信
2013/11/11 职场文书
播音主持女孩的自我评价分享
2013/11/20 职场文书
公司门卫管理制度
2014/02/01 职场文书
2015年派出所民警工作总结
2015/04/24 职场文书
小爸爸观后感
2015/06/15 职场文书
《最后一头战象》读后感:动物也有感情
2020/01/02 职场文书
Python 的 sum() Pythonic 的求和方法详细
2021/10/16 Python
SQL Server表分区降低运维和维护成本
2022/04/08 SQL Server
JavaScript实现九宫格拖拽效果
2022/06/28 Javascript