繁体   English   中英

TypeError:无法读取 null 的属性(读取“scrollHeight”)| Angular 11

[英]TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11

我真的希望你们中的一些人能帮助我解决这个令人头疼的问题。 我需要拥有页面的滚动高度,但我只得到了其中的 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

分析DOMSrollable.nativeElentscrollHeight的实际大小是 4587。因此,使用上面的 function 我不能 go 向下,因为滚动停止在 1177px。

这是我正在处理的(我的 Typescript/Angular 12)的抽象。 这是 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>

提前致谢

尝试使用getBoundingClientRect而不是scrollHeight 例如:

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.

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