Animating ui-view without position:absolute

与世无争的帅哥 提交于 2019-12-10 15:26:18

问题


I have the following structure in my Angular app:

<header></header>
<section>
    <div ui-view></div>
</section>
<footer>

Where my ui-view uses animate.css to bounce in and out of the screen. My problem is that during animation I get two instances of <div ui-view> on top of each other, pushing the first instance down. All of the examples I can find get around this by using position: absolute but since I don't know the height of the ui-view in advance and have a <footer> below my <div ui-view> which I need to display, I can't use this.

This is the way I want the animation to work, except the <footer> needs to appear below the content:

http://jsfiddle.net/9rjrdd1q/

How can I achieve this without position: absolute? Or at the very least, get my <footer> to display...


回答1:


For anyone interested, I just made a workaround using a directive which resizes the parent container to the height of the ui-view every time the state changes:

myApp.directive("fixPositionAbsolute", ["$document", "$window", function($document, $window) {
    return {
        restrict: "A",
        link: function($scope, element) {
            // Flag to determine if the directive has loaded before
            var hasLoaded;
            // DOM representation of the Angular element
            var domElement = element[0];
            $scope.$on("$stateChangeSuccess", function() {
                console.log(domElement);
                // Get the computed height of the ui-view and assign it to the directive element
                domElement.style.height = $window.getComputedStyle($document[0].querySelector("[ui-view]")).height;
                // After the first height change, add a class to enable animations from now on
                if(!hasLoaded) {
                    domElement.classList.add("auto-height");
                    hasLoaded = true;
                }
            });
        }
    };
}]);

Then added an animation for the content-wrapper's height so that it moves along with the bounce animation:

#content-wrapper.auto-height {
    height: 0;
    transition: height 0.6s ease-in-out;
}

Updated Fiddle




回答2:


Perhaps you could remove position:absolute and apply this css rule:

[ui-view].ng-leave {
    display:none;
   -webkit-animation: bounceOutLeft 1s;
}

But the leaving div will get hidden immediately:

Check the DEMO



来源:https://stackoverflow.com/questions/30074558/animating-ui-view-without-positionabsolute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!