東拼西湊完成一個「前端框架」(3) - 側邊欄

寫在前面

項目引入 
font-awesomejavascript

<link href="lib/font-awesome/css/font-awesome.min.css" rel="stylesheet" />

iconfont自定義圖標css

<link href="css/icon.css" rel="stylesheet" />

總體框架來到關鍵的部分,側邊欄菜單;html

側邊欄

收起展開動畫

說到動畫咱們能夠經過 jQuerycss3來實現,關於css3jQuery 動畫的實現方式就不詳細說了,簡單總結如下幾點:前端

  1. css3 使用 GPU,jQuery 使用 CPU
  2. css3 比 jQuery 更流暢,更快,更效率
  3. jQuery 支持全部遊覽器
  4. css3(animation, transition) 不支持 ie6, 7, 8, 9
  5. css3 animation 更靈活

綜上,仍是推薦 css3 實現動畫效果,實現動畫效果能夠經過 transitionanimation 屬性,這裏我試用 transition 屬性來實現動畫;
1. 定義transition屬性java

.ls-layout .ls-layout-slider {
    width: 200px;
    height: 100vh;
    border: none;
    background: #1f242a;
    box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
    -webkit-transition: width 300ms ease;
    transition: width 300ms ease;
    opacity: 1;
    z-index: 9;
}

2. 側邊欄 收起 樣式node

.ls-layout .ls-layout-slider.contracted {
    width: 50px;
}

.ls-layout-slider.contracted .header span {
    height: 0px;
    overflow: hidden;
}

image

3. js 改變class屬性css3

var page = page || {};
(function($) {
    $.extend(page, {
        slide: function(el) {
            var $el = $(".nav-left .operate-item.slider .ls-icon");
            var slider = $(".ls-layout-slider");
            if (slider.hasClass("contracted")) {
                slider.removeClass("contracted");
                $el.removeClass("ls-icon-shrink-right");
            } else {
                slider.addClass("contracted");
                $el.addClass("ls-icon-shrink-right");
            }
        }
    });
})(jQuery);

4. 看效果
imagegit

菜單

菜單的實現比較簡單,首先是一個樹:
cssgithub

.slider-menu ul li a:hover {
    text-decoration: none;
}

.slider-menu li ul li a {
    padding: 6px 0px 6px 20px;
}

.slider-menu li ul li ul li a {
    padding: 6px 0px 6px 30px;
}

.slider-menu li ul li ul li ul li a {
    padding: 6px 0px 6px 40px;
}

.slider-menu ul li .node:hover {
    background: rgba(0, 0, 0, .2);
    color: #fff;
}

.selected {
    border-left: 3px solid #1d7ce3;
    background: rgba(0, 0, 0, .5);
}

.selected i,
.selected b,
.selected span {
    color: #fff;
}

.selected-hover {
    border-left: 3px solid #1d7ce3;
}

.slider-menu ul li .selected:hover {
    background: rgba(0, 0, 0, .2);
}

.slider-menu .expandable {
    position: absolute;
    right: 15px;
    line-height: 20px;
}

htmlweb

...
    <ul>
        <li class="node-container node-container-root">
            <a pid="1" class="node node-root" onclick="page.expand(this)">
                <i class="fa fa-cog fa-fw" aria-hidden="true"></i>
                <span>系統管理</span>
                <b class="expandable fa fa-angle-left" aria-hidden="true"></b>
            </a>
            <ul class="node-container-son">
                <li>
                    <a pid="2" class="node " onclick="javascript:void(0);">
                        <i class="fa fa-list fa-fw" aria-hidden="true"></i>
                        <span>菜單管理</span>
                    </a>
                </li>
                <li>
                    <a pid="7" class="node " onclick="javascript:void(0);">
                        <i class="fa fa-shield fa-fw" aria-hidden="true"></i>
                        <span>權限管理</span>
                    </a>
                </li>
                <li>
                    <a pid="12" class="node " onclick="javascript:void(0);">
                        <i class="fa fa-user fa-fw" aria-hidden="true"></i>
                        <span>用戶管理</span>
                    </a>
                </li>
                <li>
                    <a pid="17" class="node " onclick="javascript:void(0);">
                        <i class="fa fa-file-text-o fa-fw" aria-hidden="true"></i>
                        <span>操做管理</span>
                    </a>
                </li>
                <li>
                    <a pid="22" class="node " onclick="javascript:void(0);">
                        <i class="fa fa-trophy fa-fw" aria-hidden="true"></i>
                        <span>角色管理</span>
                    </a>
                </li>

            </ul>
        </li>
        <li class="node-container node-container-root">
            <a pid="44" class="node node-root" onclick="page.expand(this)">
                <i class="fa fa-copy fa-fw" aria-hidden="true"></i>
                <span>日誌</span>
                <b class="expandable fa fa-angle-left" aria-hidden="true"></b>
            </a>
            <ul class="node-container-son">
                <li>
                    <a pid="45" class="node " onclick="javascript:void(0);">
                        <i class="fa fa-list-alt fa-fw" aria-hidden="true"></i>
                        <span>日誌管理</span>
                    </a>
                </li>
            </ul>
        </li>
    </ul>
...

收縮以後的樣式:

.ls-layout-slider.contracted .node-root span {
    display: none;
}

.ls-layout-slider.contracted .node-root b {
    display: none;
}

.ls-layout-slider.contracted .slider-menu .node-root {
    padding: 6px 0;
    text-align: center;
}

.ls-layout-slider.contracted .slider-menu .node-container-root {
    position: relative;
}

.ls-layout-slider.contracted .slider-menu .node-container-son {
    display: none;
    width: 150px;
    position: absolute;
    background: #1f242a;
    padding: 10px;
    padding: 10px;
    border-radius: 4px;
    left: 51px;
    top: 0;
}

.ls-layout-slider.contracted .slider-menu .node-container-root:hover .node-container-son {
    display: block;
}

.ls-layout .ls-layout-slider.contracted input {
    width: 0;
}

.ls-layout .layui-tab-title.contracted .node-container .slider-menu li ul {
    background: rgba(0,0,0,0);
}

看效果:image

基本完成,歡迎批評指正;

源碼地址

https://github.com/LaosanShang/ls-admin-frontend

相關文章
相關標籤/搜索