Table of Contents
treeview
Home Web Front-end HTML Tutorial 怎样制作web版的folder treeview_html/css_WEB-ITnose

怎样制作web版的folder treeview_html/css_WEB-ITnose

Jun 24, 2016 am 11:25 AM

文件夹treeview的效果

这样的treeview在实际项目中使用的场景较多。

既然用的多,那就DIY一遍,虽没有面面俱到,但也要将其基本实现完成一遍。

1.先准备图标素材

 file.gif,文件图标

 folder.gif,文件夹打开中的图标

 folder-closed.gif,文件夹关闭着的图标

 treeview-default.gif,折叠图标

 treeview-default-line.gif,折叠线图标,实际分辨率是16*1776

 

2.

treeview是基于ul li以及他们的嵌套,将文件夹树用列表搭出

html代码

<!DOCTYPE><html>    <head>        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>        <title>treeview</title>        <style type="text/css">        </style>    </head>    <body>            <h4 id="treeview">treeview</h4>    <ul id="browser" class="treeview">        <li>            <div class='foldarea foldarea-collapsable'></div>            <span class="folder folder-opened">文件夹1</span>            <ul>                <li class="last"><span class="file">文件1-1</span></li>            </ul>        </li>        <li>            <div class='foldarea foldarea-collapsable'></div>            <span class="folder folder-opened">文件夹2</span>            <ul>                <li>                    <div class='foldarea foldarea-collapsable'></div>                    <span class="folder folder-opened">文件夹2.1</span>                    <ul id="">                        <li><span class="file">文件2.1-1</span></li>                        <li class="last"><span class="file">文件2.1-2</span></li>                    </ul>                </li>                <li class="last"><span class="file">文件2-1</span></li>            </ul>        </li>        <li>            <div class='foldarea foldarea-collapsable'></div>            <span class="folder folder-opened">文件夹3</span>            <ul>                <li class="last"><span class="file">文件3-1</span></li>            </ul>        </li>        <li class="last"><span class="file">文件0-1</span></li>    </ul>    <script src="lib/jquery.js" type="text/javascript"></script>    <script type="text/javascript">        </script></body></html>
Copy after login

在代码中使用css的类名将各个元素的角色定义好,已备后续的css定义。

3.

一个列表的样式出来后,为了将列表的样式改成文件夹树的样式,先得把列表ul样式清空,包括嵌套的ul,将他们的padding和margin都设成0px已备后续按照自己的要求定义。

            .treeview, .treeview ul{                list-style: none;                padding: 0px;                margin: 0px;            }
Copy after login

4.

将列表的背景色设成白色,设置顶外边距保持与上面的元素一点距离。

为li设上自定义的内边距达到自定义缩进的目的。

            .treeview ul{                background-color: white;                margin-top: 4px;            }            .treeview li{                margin:0px;                padding:3px 0px 3px 16px;            }
Copy after login

5.

列表模式是展开状态。

在文件夹span前,加上加号或减号图标,使用div作为图标显示元素,默认采用可折叠样式foldarea-collapsable,显示减号图标。

        <li>            <div class='foldarea foldarea-collapsable'></div>            <span class="folder folder-opened">文件夹1</span>            <ul>                <li class="last"><span class="file">文件1-1</span></li>            </ul>        </li>
Copy after login
Copy after login
Copy after login

将该div设成左浮动,这样就可以变成可设定宽度的内联元素,和文件夹span处于一行。

            .treeview .foldarea{                height: 16px;                width: 16px;                float: left;                margin-left: -16px;            }
Copy after login

定义可折叠样式和可展开样式。

可折叠样式表示,当前列表处于展开中,显示减号图标。

可展开样式表示,当前列表处于折叠中,显示加号图标。

            .treeview  .foldarea-expandable{                background: url(images/treeview-default.gif) -80px -3px no-repeat;            }            .treeview .foldarea-collapsable{                background: url(images/treeview-default.gif) -64px -25px no-repeat;            }
Copy after login

6.

为文件夹文字前加上文件夹图标,默认采用文件夹已打开样式folder-opened,显示文件夹已打开图标。

        <li>            <div class='foldarea foldarea-collapsable'></div>            <span class="folder folder-opened">文件夹1</span>            <ul>                <li class="last"><span class="file">文件1-1</span></li>            </ul>        </li>
Copy after login
Copy after login
Copy after login

先设置文件夹文字缩进,让其向右缩进自定义的距离,为图标显示留下空间。

            .treeview .folder{                padding: 1px 0px 1px 16px;            }
Copy after login

定义文件夹已打开和已关闭样式,用来设置对应的图标。

            .treeview .folder-opened{                background: url(images/folder.gif) 0 0 no-repeat;            }            .treeview .folder-closed{                background: url(images/folder-closed.gif) 0 0 no-repeat;            }
Copy after login

7.

定义文件样式,为文件文字前加上文件图标。

        <li>            <div class='foldarea foldarea-collapsable'></div>            <span class="folder folder-opened">文件夹1</span>            <ul>                <li class="last"><span class="file">文件1-1</span></li>            </ul>        </li>
Copy after login
Copy after login
Copy after login

将文件文字缩进,为图标露出显示空间,设置文件背景图标。

            .treeview .file{                background: url(images/file.gif) 0 0 no-repeat;                padding: 0px 0px 1px 16px;            }
Copy after login

8.

为所有的li列表项设置折叠线背景

折叠线背景图的上部有个直角的小分叉。

            .treeview li {                background: url(images/treeview-default-line.gif) 0 0 no-repeat;            }
Copy after login

设置完折叠线后,每一个最后的列表项都会拖一个尾巴,为了去掉这个尾巴,折叠线背景图的最下部是一个直角的闭合样式。

并且将所有最后一个li列表项设置成last样式,表示这是最后一个列表项

    <li> <div class='foldarea foldarea-collapsable'></div> <span class="folder folder-opened">文件夹1</span> <ul> <li class="last"><span class="file">文件1-1</span></li> </ul> </li>
  • 文件夹2
    • 文件夹2.1
      • 文件2.1-1
      • 文件2.1-2
    • 文件2-1
  • 文件夹3
    • 文件3-1
  • 文件0-1
Copy after login

last样式的关键点就是把折叠线的可视部分通过background-position定位到下部的直角上,这样在效果上就把折叠线给关闭了。

            .treeview li.last {                background-position: 0 -1766px;            }
Copy after login

9.

最后,我们要在鼠标移到文件夹上后改变文字色以及鼠标指针。

那么先定义hover样式。

            .hover{                cursor: pointer;                color: red;            }
Copy after login

找到所有的文件夹span,响应hover事件,动态的添加和删除鼠标进入样式,已达到动态效果。

并且响应文件夹span和加减号div的单击事件,在展开的时候单击就折叠其下的列表,在折叠的时候单击就展开,折叠和展开是通过控制display:none样式来实现。

然后就是根据折叠或展开的状态控制图标的显示。

       $(document).ready(function(){            var folders = $('.folder');            var foldareas = $('.foldarea');            //鼠标移入文件夹节点上,节点文字变色,鼠标指针改变            folders.hover(                    function(){                        $(this).addClass('hover');                    },                    function(){                        $(this).removeClass('hover');                    }            );            var doFold = function(){                var ul = $('ul',this.parentNode)[0];                var foldarea = $('.foldarea',this.parentNode)[0];                var folder = $('.folder',this.parentNode)[0];                if(!ul){                    return;                }                var ulDisplay =   $(ul).css('display');                if(ulDisplay==='none'){                    //展开列表                    $(ul).css('display','block');                    //显示展开时的文件夹图标                    $(folder).removeClass('folder-closed');                    $(folder).addClass('folder-opened');                    //展开时显示可折叠图标                    $(foldarea).removeClass('foldarea-expandable');                    $(foldarea).addClass('foldarea-collapsable');                }else{                    //通过隐藏来实现折叠列表                    $(ul).css('display','none');                    //显示折叠时的文件夹图标                    $(folder).removeClass('folder-opened');                    $(folder).addClass('folder-closed');                    //折叠时显示可展开图标                    $(foldarea).removeClass('foldarea-collapsable');                    $(foldarea).addClass('foldarea-expandable');                }            };            //将文件夹节点下的列表折叠或展开            folders.click(doFold);            foldareas.click(doFold);        });
Copy after login

 

至此,完成了treeview的基本实现。

如果需要完成整体功能,就要在此基础之上封装实现treeview的各个功能。

 

代码:戳

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

From Text to Websites: The Power of HTML From Text to Websites: The Power of HTML Apr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles