本文实例讲述了JS实现适合于后台使用的动画折叠菜单效果。分享给大家供大家参考。具体如下:
这是一款适合于后台使用的动画折叠菜单,代码超简,无jQuery,展开与折叠效果平滑。本演示中有两组菜单,向大家展示如何在一个网页中布局两个菜单,整体效果以及兼容性还算可以,希望在实际应用中不会出现什么问题。
运行效果截图如下:
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>适合于后台使用的动画折叠菜单</title>
<script type="text/javascript">
function FOLDMenu(id,onlyone){
if(!document.getElementById || !document.getElementsByTagName){return false;}
this.menu=document.getElementById(id);
this.submenu=this.menu.getElementsByTagName("ul");
this.speed=3;
this.time=10;
this.onlyone=onlyone==true?onlyone:false;
this.links = this.menu.getElementsByTagName("a");
}
FOLDMenu.prototype.init=function(){
var mainInstance = this;
for(var i=0;i<this.submenu.length;i++){
this.submenu[i].getElementsByTagName("span")[0].> mainInstance.toogleMenu(this.parentNode);
};
}
for(var i=0;i<this.links.length;i++){
this.links[i].> this.className = "current";
mainInstance.removeCurrent(this);
}
}
}
FOLDMenu.prototype.removeCurrent = function(link){
for (var i = 0; i < this.links.length; i++){
if (this.links[i] != link){
this.links[i].className = " ";
}
}
}
FOLDMenu.prototype.toogleMenu=function(submenu){
if(submenu.className=="open"){
this.closeMenu(submenu);
}else{
this.openMenu(submenu);
}
}
FOLDMenu.prototype.openMenu=function(submenu){
var fullHeight=submenu.getElementsByTagName("span")[0].offsetHeight;
var links = submenu.getElementsByTagName("a");
for (var i = 0; i < links.length; i++){
fullHeight += links[i].offsetHeight;
}
var moveBy = Math.round(this.speed * links.length);
var mainInstance = this;
var intId = setInterval(function() {
var curHeight = submenu.offsetHeight;
var newHeight = curHeight + moveBy;
if (newHeight <fullHeight){
submenu.style.height = newHeight + "px";
}else {
clearInterval(intId);
submenu.style.height = "";
submenu.className = "open";
}
}, this.time);
this.collapseOthers(submenu);
}
FOLDMenu.prototype.closeMenu=function(submenu){
var minHeight=submenu.getElementsByTagName("span")[0].offsetHeight;
var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
var mainInstance = this;
var intId = setInterval(function() {
var curHeight = submenu.offsetHeight;
var newHeight = curHeight - moveBy;
if (newHeight > minHeight){
submenu.style.height = newHeight + "px";
}else {
clearInterval(intId);
submenu.style.height = "";
submenu.className = "";
}
}, this.time);
}
FOLDMenu.prototype.collapseOthers = function(submenu){
if(this.onlyone){
for (var i = 0; i < this.submenu.length; i++){
if (this.submenu[i] != submenu){
this.closeMenu(this.submenu[i]);
}
}
}
}
</script>
<style type="text/css">
*{margin:0;padding:0;}
.foldmenu{border:1px solid #000;width:150px;margin:50px;}
.foldmenu ul{list-style-type:none;height:25px;line-height:25px;overflow:hidden;cursor:pointer;}
.foldmenu ul.open{height:auto;}
.foldmenu ul span{display:block;padding-left:5px}
.foldmenu ul li{border-bottom:1px solid #DDDDDD;}
.foldmenu li a{background-color:#EEEEEE;color:#006666;display:block;padding:5px 10px;text-decoration:none;text-align:center;}
.foldmenu li a:hover{background-color:#006666;color:#ffffff;}
.foldmenu li a.current{background-color:#006666;color:#ffffff;}
</style>
</head>
<body>
<div id="foldmenu" class="foldmenu" style="float:left;">
<ul class="open">
<span>编程天地</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >电子书籍</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >源码乐园</a></li>
</ul>
<ul>
<span>中国门户</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >百度搜索</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >新浪新闻</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >腾讯QQ</a></li>
</ul>
<ul>
<span>音乐网站</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >百度音乐</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >QQ163</a></li>
</ul>
</div>
<div id="foldmenu2" class="foldmenu" style="float:right;">
<ul class="open">
<span>世界大学</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >剑桥</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >牛津</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >哈佛</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >麻省理工</a></li>
</ul>
<ul>
<span>中国大学</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >北大</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >清华</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >港大</a></li>
</ul>
<ul>
<span>世界好车</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >宝马</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >奔驰</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >奥迪</a></li>
</ul>
<ul>
<span>开源论坛</span>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >PhpWind</a></li>
<li><a href="#" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" rel="external nofollow noopener" >Discuz!</a></li>
</ul>
</div>
<script type="text/javascript">
window.onload = function() {
myMenu = new FOLDMenu("foldmenu",true);
myMenu.init();
myMenu2 = new FOLDMenu("foldmenu2");
myMenu2.init();
};
</script>
</body>
</html>
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台用户上传并发布,不构成投资建议请自行甄别,如有侵权请联系删除:liaycn@163.com
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of the site, which is a social media platform and only provides information storage services.