mani singh
mani singh

Reputation: 251

How to solve "Cannot read property 'scrollIntoView' of undefined"?

I want to scroll to a particular div on clicking a button in Angular 7, below is the code I am using but it is working in stackblitz but showing error when i use in my project. "Cannot read property 'scrollIntoView' of undefined".

https://stackblitz.com/edit/angular-scroll-local-variable?file=src%2Fapp%2Fscroll.component.ts

try this link: https://stackblitz.com/edit/angular-scroll-local-variable-ja96uz?file=src%2Fapp%2Fapp.component.html

<button (click)="scroll(target)"></button>
<div #target>Your target</div>

and in component:

scroll(el) {
    el.scrollIntoView();
}

Upvotes: 4

Views: 28970

Answers (4)

Prashant Pimpale
Prashant Pimpale

Reputation: 10697

The code is not working because of *ngIf condition, when you use show variable is default set to false, hence the div is not rendered on the component.

The code should be used in a related component where you want to scroll to be done, for example:

if you required in the scroll component then

HTML:

<button (click)="scroll(target)">clicking this button</button>
<div style="marging-top: 100px; height: 900px;"></div>
<div #target *ngIf="show" style="border: 1px solid #000; padding: 10px;margin-top: 10px;">
show get screen scrolled to this div
</div>

TS:

scroll(el: HTMLElement) {
   if(el){ // If the div is rendered on the HTML then it should be HTML element
     el.scrollIntoView();
  }
}

StackBlitz

Upvotes: 0

Akil Makda
Akil Makda

Reputation: 431

Scroll.html

<button (click)="scroll()">Click to scroll</button>
<div id="target">Your target</div>

componet.ts

getOffset(el) {
   el = el.getBoundingClientRect();
   return {
      left: el.left + window.scrollX,
      top: el.top + window.scrollY,
      bottom: el.top + window.scrollY
   }
}


scroll() {
    var scroll_to = document.getElementById('target');
    var topHight = this.getOffset(scroll_to).top;
    window.scrollTo(0, topHight);
  }

Upvotes: 0

Chellappan வ
Chellappan வ

Reputation: 27343

Try angular ViewportScroller Service Which provide scrollToAnchor method

scroll.html

<button (click)="scroll('target')">Click to scroll</button>
<div id="target">Your target</div>

scroll.ts

import { Component, Input } from '@angular/core';
import { ViewportScroller } from '@angular/common';
@Component({
  selector: 'scroll',
  template: `
    <button (click)="scroll('target')">Click to scroll</button>
    <div id="target">Your target</div>
  `,
  styles: [`h1 { font-family: Lato; }`, `div { margin-top: 5000px; }`]
})
export class ScrollComponent {

  constructor(private vps: ViewportScroller) {

  }
  scroll(id) {
    this.vps.scrollToAnchor(id);
  }
}

Example:https://stackblitz.com/edit/angular-scroll-local-variable-99hwvo

Upvotes: 4

De Wet van As
De Wet van As

Reputation: 1000

Try using ViewChild:

//HTML 
<button (click)="scroll()"></button><div #target>Your target</div>


//ts

//Import
import { ..., ViewChild, ElementRef } from '@angular/core';

//Declare
@ViewChild('target') targetEl: ElementRef;

scroll() {
    this.targetEl.nativeElement.scrollIntoView();
}

Upvotes: 1

Related Questions