热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

高仿CSDN社区树形图.

一直感觉CSDN社区的树形结构特别的人性化,直观化。最近做系统的时候需要用到这个树形结构,于是模仿CSDN的树形结构做了一个自己的树形结构,

一直感觉CSDN社区的树形结构特别的人性化,直观化。最近做系统的时候需要用到这个树形结构,于是模仿CSDN的树形结构做了一个自己的树形结构,将实现过程记录如下,希望对读者有用。

工欲善其事必先利其器,要模仿一个经典的树形结构就应该参照人家是怎么实现的,这里用到了firebug插件,在firefox中查看了CSDN的树形结构的组成,发现其实树形结构就是一个个的DIV借助img以及背景图片拼凑而成,整理了一下思路发现其实要想达到CSDN的效果也不是很困难。

让我们一步一步的分析

1、首先把用到的图片下载下来,一共15个图片,利用firebug工具可以很轻松的把图片下载(另存为)下来。

2、以上这些图片都是在CSS文件中作为背景图片出现的,其他地方以空白图片填充。

3、然后分析树形结构的组成,首先有一个根目录,然后每个根目录下面有其子目录,也就是说每个子目录开始的时候是隐藏的只有当鼠标点击展开的时候才会显示,在此过程中节点图标以及文件夹图标会变化。

Javascript代码:

[Javascript] view plaincopyprint?
  1.           



4、当点击展开标签的时候进行判断如果为缩略图片则转变为展开图片并且将隐藏的内容显示,以至于达到资源管理器的效果。

全部实现代码:

[html] view plaincopyprint?
  1. <html>  
  2.     <head>  
  3.         <meta http-equiv&#61;"Content-Type" content&#61;"text/html; charset&#61;GB18030">  
  4.       
  5.   
  6.   
  7.         <script language&#61;"Javascript">  
  8. function display(id) {  
  9.      eval("var div&#61;div"&#43;id);  
  10.      eval("var img&#61;img"&#43;id);  
  11.      eval("var im&#61;im"&#43;id);  
  12.      div.style.display&#61;div.style.display&#61;&#61;"block"?"none":"block";  
  13.      img.src&#61;div.style.display&#61;&#61;"block"?"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_minus1.gif":"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif";  
  14.      im.src&#61;div.style.display&#61;&#61;"block"?"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/folderopen.gif":"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/folder.gif";  
  15.      img.alt&#61;div.style.display&#61;&#61;"block"?"关闭":"展开";  
  16. }  
  17. script>  
  18.     head>  
  19.     <body class&#61;"body1" style&#61;"background-color :#E5E5E5">  
  20.     <div id&#61;"title" >div>  
  21.         <table>  
  22.             <tr>  
  23.                 <td valign&#61;"top" nowrap&#61;"nowrap">  
  24.                     <div>  
  25.                         <img alt&#61;"展开" style&#61;"cursor:hand;" onClick&#61;"display(&#39;1&#39;);"  
  26.                             id&#61;"img1" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif">  
  27.                         <img id&#61;"im1" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/folder.gif">  
  28.                         <a> 根目录a>  
  29.                         <div style&#61;"display:none;" id&#61;"div1">  
  30.                             <div style&#61;"background-image:http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line1.gif">  
  31.                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif" >  
  32.                                 <img alt&#61;"展开" style&#61;"cursor:hand;" onClick&#61;"display(&#39;2&#39;);"  
  33.                                     id&#61;"img2" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif">  
  34.                                 <img id&#61;"im2" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/folder.gif">  
  35.                                 <a>二级目录a>   
  36.                                <div style&#61;"display:none;" id&#61;"div2">  
  37.                                     <div>  
  38.                                         <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  39.                                         <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  40.                                         <img alt&#61;"展开" style&#61;"cursor:hand;" onClick&#61;"display(&#39;3&#39;);"  
  41.                                             id&#61;"img3" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif">  
  42.                                         <img id&#61;"im3" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/folder.gif">  
  43.                                         <a>三级目录a>  
  44.                                         <div style&#61;"display:none;" id&#61;"div3">  
  45.                                             <div>  
  46.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  47.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  48.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  49.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_minus1.gif">  
  50.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/file.gif">  
  51.                                                 <a>题目一a>  
  52.                                             <div>  
  53.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  54.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  55.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  56.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_minus1.gif">  
  57.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/file.gif">  
  58.                                                   
  59.                                                 <a>题目二a>  
  60.                                             div>  
  61.                                             <div>  
  62.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  63.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  64.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  65.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_minus1.gif">  
  66.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/file.gif">  
  67.                                                 <a>题目三a>  
  68.                                              div>  
  69.                                              <div>  
  70.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  71.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  72.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_line4.gif">  
  73.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_minus2.gif">  
  74.                                                 <img src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/file.gif">  
  75.                                                 <a>题目四a>  
  76.                                              div>  
  77.                                             div>  
  78.                                         div>  
  79.                                     div>                     
  80.                                 div>                                  
  81.                             div>  
  82.                         div>  
  83.                     div>  
  84.                 td>  
  85.             tr>  
  86.         table>  
  87.     body>  
  88. html>  














id&#61;"img1" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif">

根目录



id&#61;"img2" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif">

二级目录




id&#61;"img3" src&#61;"http://bbs.csdn.net/ui/scripts/System/_resource/MzTreeView/_plus1.gif">

三级目录









实现效果如下:



推荐阅读
author-avatar
55525wio
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有