简体   繁体   English

如何仅使用上方的div和下方的div动态更新div的最大高度?

[英]How can I dynamically update a div's max-height by just using the div above and the div below?

I am using twitter bootstrap, angular, and jquery to try to solve this issue. 我正在使用twitter bootstrap,angular和jquery尝试解决此问题。

I have a left hand div that has 4 other divs inside. 我有一个左手div,里面还有其他4个div。 This left hand div has a width of 300px and has a 100% height. 此左div的宽度为300px,高度为100%。

The first div starts off hidden and can change sizes because it shows one of three different error messages, as well as the selection that a user makes. 第一个div开始是隐藏的,可以更改大小,因为它显示了三种不同的错误消息之一以及用户所做的选择。 This div has a ng-show attribute as well as ng-show attributes on some of it's child elements. 该div在其某些子元素上具有ng-show属性以及ng-show属性。

The second div is always the same size, as well as the fourth div. 第二个div的大小始终与第四个div相同。 The fourth div is positioned to always be on the bottom. 第四个div始终位于底部。

The third div has a table inside of it. 第三div内部有一个表格。 I want to set a max-height on this table so that I can set overflow-y:auto on this div only. 我想在此表上设置最大高度,以便仅在此div上设置overflow-y:auto。

Is there a way to watch the bottom of the second div so that I can calculate the available height? 有没有办法观察第二个div的底部,以便我可以计算可用高度?

I have this code, however, it executes before the ng-show is finished, making it wrong most of the time: 我有这段代码,但是它在ng-show完成之前执行,这在大多数情况下是错误的:

app.directive('adjustTableHeight', ['$window', function($window) {
    return {
        restrict: 'A',
        link: function(scope, element) {
            var w = angular.element($window);
            scope.getUpperDivBottom = function () {
                var upperDiv = angular.element('#secondDiv'),
                    upperDivBottom = upperDiv && upperDiv.length ? (upperDiv.position().top + upperDiv.outerHeight(true)) : 0;
                return Math.round(upperDivBottom);
            };
            scope.$watch(scope.getUpperDivBottom, function (newValue, oldValue) {
                var lowerDiv = angular.element('#fourthDiv'),
                    lowerDivTop = lowerDiv && lowerDiv.length ? lowerDiv.position().top : 0;
                console.log('upperDivBottom', newValue, oldValue);
                console.log('lowerDivTop', lowerDivTop);
                console.log('height', Math.round(lowerDivTop) - newValue);
                element.css('max-height', Math.round(lowerDivTop) - newValue);
            }, true);
            w.on('resize', _resize);
            scope.$on('$destroy', function() {
                w.off('resize', _resize);
            });
            function _resize() {
                scope.$apply();
            };
        }
    };
}]);

So, the issue that I was having is that I was attempting to monitor items where angular has no control, like a div's position. 因此,我遇到的问题是我试图监视角度不受控制的项目,例如div的位置。 Once I modified the code to watch scope variables that are changing and added a $timeout, I stopped having all issues and everything started working well. 一旦修改了代码以查看范围变量的变化并添加了$ timeout,我就停止了所有问题,一切都开始正常运行。

scope.$watchGroup(['item1, 'item2'],
    function(newValues, oldValues, scope) {
        $timeout(function() {
           setMaxHeight();
        }, 0);
    }
);

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

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