简体   繁体   English

角 6 | 路由器链接

[英]Angular 6 | RouterLink

有什么方法可以在不重新加载的情况下转到由 id 定义的另一个页面的部分

There were a bunch of new scrolling features added in Angular v6.1. Angular v6.1 添加了许多新的滚动特性。

Here is a quote from a blog post about it:这是一篇关于它的博客文章的引用:

The Router Scroller provides functions related to scrolling to the Angular Router. Router Scroller 提供与滚动到 Angular Router 相关的功能。 With Router Scroller, you can do the following things.使用 Router Scroller,您可以执行以下操作。

Restore to scroll position before transition when browser back Fragmented URL like #foo and automatically scroll to elements with corresponding ID当浏览器返回像#foo这样的#foo片URL时恢复到过渡前的滚动位置并自动滚动到具有相应ID的元素

I assume the second paragraph (anchor scrolling) is what you are asking about?我假设第二段(锚滚动)是你要问的?

If so, you can find out more here:如果是这样,您可以在此处了解更多信息:

https://medium.com/lacolaco-blog/introduce-router-scroller-in-angular-v6-1-ef34278461e9 https://medium.com/lacolaco-blog/introduce-router-scroller-in-angular-v6-1-ef34278461e9

And

https://blog.ninja-squad.com/2018/07/26/what-is-new-angular-6.1/ https://blog.ninja-squad.com/2018/07/26/what-is-new-angular-6.1/

You can do using scrollIntoView method您可以使用 scrollIntoView 方法

Credits goes to:ibenjelloun致谢:ibenjelloun

Example: https://stackblitz.com/edit/angular-scroll-spy-routing示例: https : //stackblitz.com/edit/angular-scroll-spy-routing

@HostListener('scroll', ['$event'])
onScroll(event: any) {
    let currentSection: string;
    const children = this._el.nativeElement.children;
    const scrollTop = event.target.scrollTop;
    const parentOffset = event.target.offsetTop;
    for (let i = 0; i < children.length; i++) {
        const element = children[i];
        if (this.spiedTags.some(spiedTag => spiedTag === element.tagName)) {
            if ((element.offsetTop - parentOffset) <= scrollTop) {
                currentSection = element.id;
            }
        }
    }
    if (currentSection !== this.currentSection) {
        this.currentSection = currentSection;
        this.sectionChange.emit(this.currentSection);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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