Smooth scroll angular2

前端 未结 9 2183
旧巷少年郎
旧巷少年郎 2020-12-24 07:47

I am having trouble getting a smooth scroll service to work in angular 2. Are there any services for smooth scrolling, or plain anchor scrolling, that might work until the

相关标签:
9条回答
  • 2020-12-24 08:14

    For anyone still on the search for a smooth scroll @alex-j 's answer works great for me in Angular 2.0 - but I had to change the Window service to this :-

    import { Injectable } from '@angular/core';
    
    function _window() : any {
        // return the global native browser window object
        return window;
    }
    
    @Injectable()
    export class WindowRef {
        get nativeWindow() : any {
            return _window();
        }
    }
    

    All props to this blog http://juristr.com/blog/2016/09/ng2-get-window-ref/ - now I have a smooth scroll service I can call from anywhere :)

    0 讨论(0)
  • 2020-12-24 08:17

    If you want a very simple anchor jump that works after routing and within routed views, you can also use ng2-simple-page-scroll.

    <a simplePageScroll href="#myanchor">Go there</a>
    

    Or right after routing:

    <a simplePageScroll [routerLink]="['Home']" href="#myanchor">Go there</a>
    

    It does a simple instant jump, but it works.

    0 讨论(0)
  • 2020-12-24 08:20

    example:

    function goToElement(elemId){
     let element = window.getElementById(elemId);
     element.scrollIntoView({behavior: "smooth"});
    }
    
    0 讨论(0)
提交回复
热议问题