AngularJS获取没有jquery的文档高度

时间:2016-03-11 12:59:51

标签: angularjs

正如标题所说,你如何获得文件高度? 我尝试了很多东西,但没有成功。 我需要这样做的原因是因为如果元素不在页面上,我有一个附加指令并且修复和元素到页面底部。 文件的高度虽然可以改变(由于像手风琴一样)。 这是我的指示:

.directive('kdActionBar', ['$timeout', '$document', '$window', function ($timeout, $document, $window) {

    // Adds the class if necessary
    var _addFixedClass = function (element, position, scrollY) {

        // If our scroll position is greater than our element position
        if (scrollY > position) {

            // Remove our class to our element
            element.removeClass('navbar-fixed-bottom');

            // If our scroll position is less than or equal to our element position
        } else {

            // Add our class to our element
            element.addClass('navbar-fixed-bottom');
        }
    };

    return {
        restrict: 'A',
        link: function (scope, element) {

            // Fun after the page has loaded
            $timeout(function () {

                // Get our elements position
                var elementPos = element.prop('offsetTop') - $window.innerHeight,
                    documentHeight = $document.innerHeight;

                // Call our function when the page has loaded
                _addFixedClass(element, elementPos - height, $window.scrollY);

                // Bind to our window scroll position
                angular.element($window).bind("scroll", function () {

                    // Get our window height
                    var newHeight = $document.innerHeight,
                        newPosition = documentHeight !== newHeight ? elementPos + (newHeight - documentHeight) : elementPos;

                    // Add or remove our class
                    _addFixedClass(element, newPosition, $window.scrollY);
                });
            });
        }
    };
}])

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

getName()