JS技术

JavaScript中伸缩菜单的制作_Javascript教程

字号+ 作者:H5之家 来源:H5之家 2015-10-02 18:02 我要评论( )

JavaScript中伸缩菜单的制作,学习JavaScript中伸缩菜单的制作,JavaScript中伸缩菜单的制作,查看JavaScript中伸缩菜单的制作,JavaScript中伸缩菜单的制作下面是纯

                                             JavaScript中伸缩菜单的制作

下面是纯JavaScript+CSS制作的二级伸缩菜单,简单易懂,具有很强的定制性和重复开发性,希望对大家有所帮助....

程序说明:

 

    .....
  <!--加载程序-->
<body onload = "initMenu()">
  <!--此处须定义ID-->
  <div class="jsmenu" id="pm_menu">
    <!--展开的一级分类-->
    <div>
      <span class="fath">业界资讯</span>
      <p class="son">
        <!--class="current"当前所在二级分类-->
        <a class="current" href="#">业界新闻</a>
        <a href="#">技术动态</a>
        <a href="#">八卦评论</a>
      </p>
     </div>
     <!--闭合的一级分类-->
     <div class="collapsed">
      
       ....

     </div>
     <!--独立的一级分类-->
     <div><span class="single"><a href="#">酷站</a></span></div>

    ....
 


程序说明:

以下为引用的内容:

//在ID为“js_menu”的div内开始程序
function initMenu(){
      var pm_menu = new JSMenu("js_menu");
        pm_menu.init();
    }
//定义主函数
function JSMenu(id) {
    if (!document.getElementById || !document.getElementsByTagName)
        return false;
    this.menu = document.getElementById(id);
    this.submenus = this.menu.getElementsByTagName("div");
}
//引入函数,取得所有span
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();
};
//展开含"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]);
            }
        }
};
//变换菜单状态函数
JSMenu.prototype.toggleMenu = function(submenu) {
    if (submenu.className == "collapsed")
        this.expandMenu(submenu);
    else
        this.collapseMenu(submenu);
};
//展开所有菜单函数
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);
};
//折叠菜单函数
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);
};
//折叠其他菜单函数
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]);
};
 

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "">
<html xmlns="">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>JavaScript可伸缩菜单 - Oncoding编码营原创</title>
  <!--引入JS文件-->
  <script type="text/javascript" src="jsmenu.js"></script>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JavaScript入门教程(二)_javascript教程教程

    JavaScript入门教程(二)_javascript教程教程

    2015-10-10 14:25

  • JavaScript入门教程(五)_javascript教程教程

    JavaScript入门教程(五)_javascript教程教程

    2015-10-10 14:21

  • JavaScript入门教程(四)_javascript教程教程

    JavaScript入门教程(四)_javascript教程教程

    2015-10-10 14:19

  • JavaScript入门教程(三)_javascript教程教程

    JavaScript入门教程(三)_javascript教程教程

    2015-10-10 14:17

网友点评