JS collapse other Menu functions

Source: Internet
Author: User
Tags setinterval

<body onload = "Initmenu ()"
  <!--you must define ID-->
  <div class= "Jsmenu" id= "Pm_menu"
    <!--The first level of classification;
    <div>
       <span class= "fath" > Industry information </span>
      <p class= "Son"
        <!--class= "current" is currently in level two classification;
         <a class= "Current" href= "#" > Industry news </a>
        <a href= "#" > Technology dynamic </A>
        <a href= "#" > Gossip review </a>
      </p>
     </div>
      <!--closed level classification;
     <div class= "collapsed"
       
       ...

</div>
<!--independent First class classification-->
<div><span class= "single" ><a href= "#" > Cool station </a></span></div>

....


Program Description:

The following are the referenced contents:

Start a program in a div with id "js_menu"
function Initmenu () {
var pm_menu = new Jsmenu ("Js_menu");
Pm_menu.init ();
}
Defining the main function
function Jsmenu (ID) {
if (!document.getelementbyid | |!document.getelementsbytagname)
return false;
This.menu = document.getElementById (ID);
This.submenus = This.menu.getElementsByTagName ("div");
}
Introduce a function to get all spans
JSMenu.prototype.init = function () {
var maininstance = this;
for (var i = 0; i < this.submenus.length; i++)
This.submenus[i].getelementsbytagname ("span") [0].onclick = function () {
Maininstance.togglemenu (This.parentnode);
};
This.expandone ();
};
Expand the menu with "current"
JSMenu.prototype.expandOne = function () {
for (var i = 0; i < this.submenus.length; i++) {
var links = this.submenus[i].getelementsbytagname ("a");
for (var j = 0; J < Links.length; J + +) {
if (Links[j].classname = = "current")
This.expandmenu (This.submenus[i]);
}
}
};
Transform Menu State function
JSMenu.prototype.toggleMenu = function (submenu) {
if (Submenu.classname = = "collapsed")
This.expandmenu (submenu);
Else
This.collapsemenu (submenu);
};
Expand All Menu Functions
JSMenu.prototype.expandMenu = 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 (5 * 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 = "";
}
}, 30);
This.collapseothers (submenu);
};
Collapse Menu function
JSMenu.prototype.collapseMenu = function (submenu) {
var minheight = Submenu.getelementsbytagname ("span") [0].offsetheight;
var moveby = Math.Round (5 * 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 = "collapsed";
}
}, 30);
};
Collapse other menu functions
JSMenu.prototype.collapseOthers = function (submenu) {
for (var i = 0; i < this.submenus.length; i++)
if (This.submenus[i]!= submenu && this.submenus[i].classname!= "collapsed")
This.collapsemenu (This.submenus[i]);
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.