获取计算的scrollY和容器元素的高度

时间:2019-03-21 19:52:36

标签: javascript html angular angular1.6

在Angular JS 1.6(我知道它很旧-旧版软件)中,我想模拟无限的产品滚动。如果用户滚动到列出的所有产品的底部,则将调用下一个分页API,并将元素附加到产品数组。

注意:我要在其上执行这些计算的div是指令模板元素

TL; DR:两个问题:

1)我需要计算出的容器元素height

2)我需要容器元素的scrollY,而不是$ window


检查用户是否滚动到显示的产品的最后一行应通过以下方式计算:

1)获取包含所有产品的容器元素的高度(我们叫容器元素.products-container

2)检查$(.products-container).scrollY> $(.products-container).height


不幸的是,我似乎无法获得scrollY的{​​{1}}或height,因为它的高度是由内部元素的数量计算的。

因此,我不得不将$(.products-container)的高度计算为其中的元素行的高度之和:

1)获取单个产品元素的高度(300px)

2)获取产品行数(每行3个产品)

3)从其中所有产品元素行中获取包含所有产品的容器元素的代理高度(我们称容器元素$(.products-container))为总和

4)检查.products-container> $window.scrollY

numberOfRows * productElementHeight

问题是:

1)计算得出的容器元素的let productElement = $('.product-item'); let productElementHeight = productElement.height(); let numberOfRows = Math.ceil(scope.products.length/productsDisplayedPerRow); let heightOfPageToLastElement = numberOfRows * productElementHeight; if (newScrollHeight > heightOfPageToLastElement) { // append next set of product results } 始终为height

2)我似乎只能获得窗口的0

scrollY

$scope.$watch(function () { return $window.scrollY; }, function (scrollY) { $rootScope.scrollHeight = scrollY; }); 始终以scrollY为目标。


注意:0$('.products-container').height()等都会产生document.getElementById('#products-container').clientHeight

0 个答案:

没有答案
相关问题