JS做的树导航控件
Skylin 近段时间根据公司的需要,使用JS做了一个树导航控件,如下图:
手动添加
- window.onload = function()
- {
- var navigation = new Navigation(document.getElementById(‘aa’),‘我的导航’);
- navigation.addLayer(‘学校管理’);
- navigation.addLayer(‘教师管理’);
- navigation.addLayer(‘杨潇管理’);
- navigation.getTree(0).createRootNode(1000,‘我是谁’,undefined,‘aa’);
- navigation.getTree(0).createRootNode(1001,‘我是杨潇’);
- navigation.getTree(1).createRootNode(1001,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).createChildNode(1002,‘我是杨潇’);
- navigation.getTree(0).getRootNode(0).getChildNode(0).createChildNode(1002,‘我是郑宇’);
- navigation.getTree(0).getRootNode(0).getChildNode(0).getChildNode(0).parentNode.parentNode.createChildNode(1002,‘我是丁双文’,”,‘aa’);
- }
- function aa()
- {
- window.location = ‘www.google.com’;
- }
基本采用DOM风格来进行编写的。下面说明一下各个函数参数的用法。
- /*
- * 导航构造函数
- * targetObj:目标对象,导航显示区域
- * title:标题名称
- */
- Navigation(targetObj,title)
- /*
- * 添加层级
- * layerTitle:层级标题文本
- * isShow:添加的层级是否打开;如果所有层级不填写,则默认第一个是打开状态
- */
- addLayer(layerTitle,isShow)
- /*
- * 获取层级树
- * num:层级索引,获取指定层级的树节点
- */
- getTree(num)
- /*
- * 创建主节点
- * nodeId:节点id,可以用来存储数据库中的ID值
- * nodeText:节点文本,用来显示
- * picStyle:节点样式
- * func:点击回调的函数
- */
- createRootNode(nodeId,nodeText,picStyle,func)
- /*
- * 获取主节点
- * num:主节点索引,可以用来获取指定主节点
- */
- getRootNode(num)
- /*
- * 创建子节点
- * nodeId:节点id,可以用来存储数据库中的ID值
- * nodeText:节点文本,用来显示
- * picStyle:节点样式
- * func:点击回调的函数
- */
- createChildNode(nodeId,nodeText,picStyle,func)
- /*
- * 获取子节点
- * num:节点索引,可以用来获取指定子节点
- */
- getChildNode(num)
以上是手动操作的例子和相关的方法参数说明。
另外,我们还可以使用XML来形成树导航,XML格式如下:
文件:tree.xml
- <?xml version="1.0" encoding="utf-8"?>
- <Navigation title="我的导航1">
- <Layer title="学校管理" isShow="">
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func="">
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func=""/>
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func=""/>
- </Node>
- <Node nodeId="" title="我是郑宇" picStyle="tree-map-img-show" func="">
- <Node nodeId="" title="我是郑宇" picStyle="tree-map-img-show" func="">
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func="aa"/>
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func=""/>
- </Node>
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func=""/>
- <Node nodeId="" title="我是谁" picStyle="tree-map-img-show" func=""/>
- </Node>
- </Layer>
- <Layer title="教师管理" isShow="">
- <Node nodeId="" title="我是杨潇" picStyle="" func="">
- <Node nodeId="" title="我是谁" picStyle="" func=""/>
- <Node nodeId="" title="我是谁" picStyle="" func=""/>
- </Node>
- </Layer>
- <Layer title="教师管理" isShow="">
- <Node nodeId="" title="我是杨潇" picStyle="" func="">
- <Node nodeId="" title="我是谁" picStyle="" func=""/>
- <Node nodeId="" title="我是谁" picStyle="" func=""/>
- </Node>
- </Layer>
- <Layer title="教师管理" isShow="">
- <Node nodeId="" title="我是杨潇" picStyle="" func="">
- <Node nodeId="" title="我是谁" picStyle="" func=""/>
- <Node nodeId="" title="我是谁" picStyle="" func=""/>
- </Node>
- </Layer>
- </Navigation>
XML格式基本如上例,和相关方法参数对应,比较简单,这里就不做详细介绍了。
下面是使用XML文件创建的代码:
- window.onload = function()
- {
- var navigation = new Navigation(document.getElementById(‘aa’),‘我的导航’);
- navigation.load(‘tree.xml’);
- }
- function aa()
- {
- window.location = ‘www.google.com’;
- }
都比较简单。这里也不多讲了:)
下面是例子的链接地址:
http://www.oursit.com/example/tree2/
下载实例:http://www.oursit.com/example/tree2/tree2.rar
实例里面包含4个js文件,其实这4个JS文件都是相同的。
navigation-max.js 有动画效果(未压缩)
navigation.js 有动画效果(已压缩)
navigation-1.js 无动画效果(未压缩)
navigation-1-min.js 无动画效果(已压缩)
如果您有什么问题或疑问,欢迎一起讨论:)
Posted in JAVASCRIPT |