[英]TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11
I really hope that some of you could help me to resolve this head-scratcher.我真的希望你们中的一些人能帮助我解决这个令人头疼的问题。 I need to have the scrollHeight of the page, but I get just 1/3 of that.
我需要拥有页面的滚动高度,但我只得到了其中的 1/3。
setSize() {
let DOMScrollable = this.elementRef.nativeElement.querySelector("div[data-scroll]");
document.body.style.height = DOMScrollable.scrollHeight + "px";
}
console.log(this.dataScroll.nativeElement.scrollHeight)
// 1177
Analyzing the DOMSrollable.nativeElent
, the real size of scrollHeight
is 4587. So, using the function above I can't go down, because the scroll stops on 1177px.分析
DOMSrollable.nativeElent
, scrollHeight
的实际大小是 4587。因此,使用上面的 function 我不能 go 向下,因为滚动停止在 1177px。
Here's an abstraction of (my Typescript/Angular 12) what I'm dealing with.这是我正在处理的(我的 Typescript/Angular 12)的抽象。 This is the HTML (On my page I have 8 images)
这是 HTML(在我的页面上我有 8 张图片)
<main main>
<div data-scroll #dataScroll>
<div class="page">
<header>
<h1>Oceans</h1>
<img src="../assets/images/header.jpg" alt="">
</header>
<div class="grid">
<a href="#" class="item item_v">
<div class="item__image">
<img src="../assets/images/1.jpg" alt="">
<div class="item__meta">December 23, 2020</div>
</div>
<h2 class="item__title">Octopus punches fish in the head (just because it can)</h2>
<p>Octopuses sometimes partner with fish to hunt, but the partnership comes with risks (for the fish, that is).</p>
</a>
</div>
</div>
</div>
</main>
Thanks in advance提前致谢
Try using getBoundingClientRect
instead of scrollHeight
.尝试使用
getBoundingClientRect
而不是scrollHeight
。 eg:例如:
setSize() {
let DOMScrollable = this.elementRef.nativeElement.querySelector("div[data-scroll]");
const rect = DOMScrollable.getBoundingClientRect();
document.body.style.height = rect.height + "px";
}
console.log(rect.height)
// 4587
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.