Element.prop('scrollHeight') 为 Angular.js 中的不同列表项返回相同的值

Element.prop('scrollHeight') returns the same value for different list items in Angular.js

本文关键字:列表 返回 Angular scrollHeight prop js Element      更新时间:2023-09-26

我正在尝试获取列表项的高度和宽度。我已经检查了获取项目高度和宽度的各种方法,但没有一种返回元素提供的实际 scrollHeight。

当我通过控制台检查滚动高度时,我得到

[div.class-name, context: ...]
    0: div.className.className2
        accessKey: ""
        ....
        scrollHeight: 176
        scrollWidth: 276
        ....

所以我使用以下代码打印出来:

angular.module('app').directive('temp', function () {
    return {
        restrict: 'E',
        templateUrl: 'link/to/template',
        replace: 'true',
        link: function(scope, element) {
            console.log(element);
            console.log(element.prop('scrollHeight'));
            console.log(element.prop('scrollWidth'));
        }
    };
});

它返回

112 (height)
304 (width)

并且为无序列表中的每个列表项返回这组值。没有一个列表项具有这样的高度/宽度(即使考虑到未应用某些边距/填充值(

我需要这些值来更新 css。还有其他办法吗?或者你能给我一个提示,说明我做错了什么吗?

给我有关要求的更多详细信息,否则我认为最好基于 window.scroll
尝试 功能这意味着基于滚动,您必须将自己的CSS应用于该元素。