javascript - Angular 14 - Scroll to div with specific id - Stack Overflow

I want to scoll to a div after my view has initialized the first time.My problem so far is that AfterV

I want to scoll to a div after my view has initialized the first time. My problem so far is that AfterViewInit leads to a problem, because its not fully rendered. (Fetched data from server, then render that..)

Code so far:

  scrollTo(): void {
    if (!this.jumpedToMessage && this.jumpMessage != "0") {
      const element = document.getElementById(this.jumpMessage);
      // @ts-ignore
      element.scrollIntoView({behavior: 'auto', block: 'center', inline: 'nearest'});
      this.jumpedToMessage = true
    }
  }

Error code in console:

my-ponent.ts:58 ERROR TypeError: Cannot read properties of null (reading 'scrollIntoView') at my-ponent.scrollTo (my-ponentponent.ts:76:15) at Object.next (my-ponentponent.ts:65:18) at ConsumerObserver.next (Subscriber.js:91:33) at SafeSubscriber._next (Subscriber.js:60:26) at SafeSubscriber.next (Subscriber.js:31:18) at map.js:7:24 at OperatorSubscriber._next (OperatorSubscriber.js:13:21) at OperatorSubscriber.next (Subscriber.js:31:18) at filter.js:6:128 at OperatorSubscriber._next (OperatorSubscriber.js:13:21)

Any ideas how to solve this?

Important: Initialize only once after the first rendering

I want to scoll to a div after my view has initialized the first time. My problem so far is that AfterViewInit leads to a problem, because its not fully rendered. (Fetched data from server, then render that..)

Code so far:

  scrollTo(): void {
    if (!this.jumpedToMessage && this.jumpMessage != "0") {
      const element = document.getElementById(this.jumpMessage);
      // @ts-ignore
      element.scrollIntoView({behavior: 'auto', block: 'center', inline: 'nearest'});
      this.jumpedToMessage = true
    }
  }

Error code in console:

my-ponent.ts:58 ERROR TypeError: Cannot read properties of null (reading 'scrollIntoView') at my-ponent.scrollTo (my-ponent.ponent.ts:76:15) at Object.next (my-ponent.ponent.ts:65:18) at ConsumerObserver.next (Subscriber.js:91:33) at SafeSubscriber._next (Subscriber.js:60:26) at SafeSubscriber.next (Subscriber.js:31:18) at map.js:7:24 at OperatorSubscriber._next (OperatorSubscriber.js:13:21) at OperatorSubscriber.next (Subscriber.js:31:18) at filter.js:6:128 at OperatorSubscriber._next (OperatorSubscriber.js:13:21)

Any ideas how to solve this?

Important: Initialize only once after the first rendering

Share Improve this question asked Nov 26, 2022 at 17:29 TimoTimo 681 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Maybe you can try a different approach to achieve your goal, by using @ViewChild, which would be more Angular-like anyway.

In your HTML:

<p #scrollElement>Scroll here</p>

Then in your TS-File:

 @ViewChild("scrollElement")
 scrollEl: ElementRef;

 ngAfterViewInit() {
    this.scrollEl.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745395549a4625861.html

相关推荐

  • javascript - Angular 14 - Scroll to div with specific id - Stack Overflow

    I want to scoll to a div after my view has initialized the first time.My problem so far is that AfterV

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信