简体   繁体   English

如何在AngularJS中对菜单打开/关闭进行动画处理

[英]How to animate menu open/close in AngularJS

I'm trying to determine the 'right' way to solve this problem. 我正在尝试确定解决此问题的“正确”方法。

I have a menu with sub-menu's. 我有一个带有子菜单的菜单。 I want to animate (using jQuery transitions) a slide-up & a slide-down effect when my route changes. 我想在路线改变时动画化(使用jQuery转换)上滑和下滑效果。

I understand I shouldn't manipulate my DOM from my controller as this is a bad-practice. 我知道我不应该从我的控制器操作DOM,因为这是一种不良做法。 Should I be using directives that listen to route changes? 我应该使用监听路由更改的指令吗? Services? 服务?

(example) (例)

ul
  li
  li
  li
    ul
      li
      li

在此处输入图片说明

You should take a look at ngAnimate. 您应该看看ngAnimate。 There is a tutorial here 有一个教程在这里

You will then be able to specify animations decleratively on elements that should be animated. 然后,您将可以在应设置动画的元素上以决定性的方式指定动画。

For example you might specify "enter" and "leave" animations on a element that is dynamically shown/hidden based on a value in the scope. 例如,您可以在根据范围中的值动态显示/隐藏的元素上指定“输入”和“离开”动画。 Animations can then be specified in CSS3 (recommended), or using jQuery, mootools and other libraries. 然后可以在CSS3中(推荐)或使用jQuery,mootools和其他库来指定动画。

<li ng-repeat="item in parent.items" ng-animate="'slide'" ng-show="parent.open">{{item.text}}</li>

EDIT: This API is deprecated but it is a even better API and and similar approach. 编辑:不赞成使用此API,但它是一个更好的API和类似的方法。 Read more about it here: http://www.yearofmoo.com/2013/08/remastered-animation-in-angularjs-1-2.html . 在此处了解更多信息: http : //www.yearofmoo.com/2013/08/remastered-animation-in-angularjs-1-2.html

This is very similar to: Slide up/down effect with ng-show and ng-animate 这非常类似于: 使用ng-show和ng-animate向上/向下滑动效果

This directive: https://github.com/EricWVGG/AngularSlideables will create an easy API to add sliding to any elements. 该指令: https : //github.com/EricWVGG/AngularSlideables将创建一个简单的API,以向任何元素添加滑动。

If you're willing to you jQuery: https://github.com/onokumus/metisMenu can create expandable menus for you. 如果您愿意使用jQuery: https : //github.com/onokumus/metisMenu可以为您创建可扩展菜单。

If you're populating your menu from the backend, something like the following in your menu controller will let you set up your navigation and then instantiate the menu. 如果您是从后端填充菜单,则菜单控制器中的以下内容将使您设置导航,然后实例化菜单。

       $scope.response = null;
       $http.get(ENV.api.endpoint+"/menu").
        then(function(response) {
            $scope.navItems = response.data;
            $timeout(function() {
               jQuery('.metismenu').metisMenu();
            });
        }, function(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });

My navigation angular code is: (Note the :: in navItems makes it only populate one time.) 我的导航角度代码是:(请注意navItems中的::使其仅填充一次。)

    <ul side-navigation class="nav metismenu" id="side-menu" >
        <li ui-sref-active="active" ng-repeat="Item in ::navItems.Regular">
            <a ui-sref="{{Item.state}}"><i class="fa {{Item.icon}}"></i> <span class="nav-label">{{Item.title}}</span></a>
        </li>
        <li ng-class="{active: $state.includes(Item.state)}" ng-repeat="Item in ::navItems.DropDown">
            <a href=""><i class="fa" ng-class="Item.icon"></i> <span class="nav-label">{{Item.title}}</span><span class="fa arrow"></span></a>
            <ul class="nav nav-second-level collapse" ng-class="{in: $state.includes(Item.state)}">
                <li ui-sref-active="active" ng-repeat="Subitem in Item.items">
                    <a ui-sref="{{Subitem.state}}"><i class="fa {{Subitem.icon}}"></i> {{Subitem.title}}</a>
                </li>
            </ul>
        </li>
    </ul>

The JSON then looks like: JSON如下所示:

{
    "Regular": [
        {
            "title": "Directory",
            "icon": "fa-archive",
            "state": "directory"
        }
    ],
    "DropDown": [
        {
            "title": "Accounting",
            "icon": "fa-dollar",
            "state": "accounting",
            "items": [
                {
                    "title": "Approve Time",
                    "state": "accounting.staffTimeclockActApprove",
                    "icon": "fa-check-circle-o"
                },
                {
                    "title": "Notify Time",
                    "state": "accounting.staffTimeclockActNotify",
                    "icon": "fa-envelope-o"
                }
            ]
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM