Scroll to element on click in Angular 4

后端 未结 12 978
逝去的感伤
逝去的感伤 2020-11-29 18:57

I want to be able to scroll to a target when a button is pressed. I was thinking something like this.

相关标签:
12条回答
  • 2020-11-29 19:11

    There is actually a pure javascript way to accomplish this without using setTimeout or requestAnimationFrame or jQuery.

    In short, find the element in the scrollView that you want to scroll to, and use scrollIntoView.

    el.scrollIntoView({behavior:"smooth"});

    Here is a plunkr.

    0 讨论(0)
  • 2020-11-29 19:16

    You could do it like this:

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

    and then in your component:

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

    Edit: I see comments stating that this no longer works due to the element being undefined. I created a StackBlitz example in Angular 7 and it still works. Can someone please provide an example where it does not work?

    0 讨论(0)
  • 2020-11-29 19:17

    Jon has the right answer and this works in my angular 5 and 6 projects.

    If I wanted to click to smoothly scroll from navbar to footer:

    <button (click)="scrollTo('.footer')">ScrolltoFooter</button>
    <footer class="footer">some code</footer>
    
    scrollTo(className: string):void {
       const elementList = document.querySelectorAll('.' + className);
       const element = elementList[0] as HTMLElement;
       element.scrollIntoView({ behavior: 'smooth' });
    }
    

    Because I wanted to scroll back to the header from the footer, I created a service that this function is located in and injected it into the navbar and footer components and passed in 'header' or 'footer' where needed. just remember to actually give the component declarations the class names used:

    <app-footer class="footer"></app-footer>
    
    0 讨论(0)
  • 2020-11-29 19:20

    Another way to do it in Angular:

    Markup:

    <textarea #inputMessage></textarea>
    

    Add ViewChild() property:

    @ViewChild('inputMessage')
    inputMessageRef: ElementRef;
    

    Scroll anywhere you want inside of the component using scrollIntoView() function:

    this.inputMessageRef.nativeElement.scrollIntoView();
    
    0 讨论(0)
  • 2020-11-29 19:21

    In Angular 7 works perfect

    HTML

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

    In component

    scroll(el: HTMLElement) {
        el.scrollIntoView({behavior: 'smooth'});
    }
    
    0 讨论(0)
  • 2020-11-29 19:22

    Here is how I did it using Angular 4.

    Template

    <div class="col-xs-12 col-md-3">
      <h2>Categories</h2>
      <div class="cat-list-body">
        <div class="cat-item" *ngFor="let cat of web.menu | async">
          <label (click)="scroll('cat-'+cat.category_id)">{{cat.category_name}}</label>
        </div>
      </div>
    </div>
    

    add this function to the Component.

    scroll(id) {
      console.log(`scrolling to ${id}`);
      let el = document.getElementById(id);
      el.scrollIntoView();
    }
    
    0 讨论(0)
提交回复
热议问题