我在我的项目中使用了ng-bootstrap [ngbScrollSpy]指令,就像在the documentation中提到的那样,但它不起作用-在滚动上,活动项没有改变.

我的代码如下:

<div>
    <div class="sticky-top">
        <ul class="nav menu-sidebar">
            <li >
                <a [ngbScrollSpyItem]="[spy, 'about']">About</a>
            </li>
            <li >
                <a [ngbScrollSpyItem]="spy" fragment="schedule">Schedule</a>
            </li>
            <li >
                <a [ngbScrollSpyItem]="spy" fragment="hotel">Information about the hotel</a>
            </li>
        </ul>
    </div>

    <div ngbScrollSpy #spy="ngbScrollSpy" >
        <section ngbScrollSpyFragment="about">
            <h3>About</h3>
            <p>{{some long long text and content}}</p>
        </section>
        <section ngbScrollSpyFragment="schedule">
            <h3>Schedule</h3>
            <p>{{some long long text and content}}</p>
        </section>
        <section ngbScrollSpyFragment="hotel">
            <h3>Information about the hotel</h3>
            <p>{{some long long text and content}}</p>
        </section>
    </div>
</div>

我看到in this stackoverflow question,我的问题是我没有向div提供height,这是真的.

但我的卷轴间谍部分遍布整个页面,而不是一个小部分(NAV本身就是sticky-top).所以我不能给它高度.

我知道还有另一种方法-刷新窗口滚动上的Scrollspy,但我找不到正确的代码来帮助我.

你能解决我的问题吗? 为我提供刷新滚动的代码spy/给我有关高度的提示/帮助我找到另一个相应的元素.

非常感谢!

将链接附加到stackblitz demo

推荐答案

由于它似乎无法满足我的需求,我决定通过两个指令创建自己的scrollspy元素:

  1. scrollTo指令将负责滚动到按下li链接的部分
import { Directive, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[scrollTo]'
})
export class ScrollToDirective {

  @Input() target = '';
  constructor() { }

  @HostListener('click')
  onClick() {
    const targetElement = document.querySelector(this.target);
    if(targetElement)
      targetElement.scrollIntoView({block: 'start',behavior: 'smooth', inline:'nearest'});
  }
}

  1. scrolledTo指令将负责在滚动到当前元素时进行检测
import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[scrolledTo]',
  exportAs: 'scrolledTo'
})
export class ScrolledToDirective {
  @Input() isLast:boolean = false;
  focus = false;

  constructor(public el: ElementRef) { }

  @HostListener('window:scroll', ['$event'])
  onWindowScroll() {
    const elementPosition = this.el.nativeElement.offsetTop;
    const elementHeight = this.el.nativeElement.clientHeight;

    //you can change the check according to your requirements
    const scrollPosition = window.pageYOffset - window.screen.height / 2 ;
    this.focus = scrollPosition >= elementPosition && scrollPosition <= elementPosition + elementHeight;
    if(this.isLast)
      this.focus = scrollPosition >= elementPosition;

  }
}

而html将如下所示

<div>
    <div class="sticky-top">
        <ul class="nav menu-sidebar">
            <li >
                <a scrollTo target="#about" [class.active]="scrolledToElement1.focus">About</a>
            </li>
            <li >
                <a scrollTo target="#schedule" [class.active]="scrolledToElement2.focus">Schedule</a>
            </li>
            <li >
                <a scrollTo target="#hotel" [class.active]="scrolledToElement3.focus">Information about the hotel</a>
            </li>
        </ul>
    </div>

    <div >
        <section scrolledTo #scrolledToElement1="scrolledTo" id="about">
            <h3>About</h3>
            <p>{{some long long text and content}}</p>
        </section>
        <section scrolledTo #scrolledToElement2="scrolledTo" id="schedule">
            <h3>Schedule</h3>
            <p>{{some long long text and content}}</p>
        </section>
        <section scrolledTo #scrolledToElement3="scrolledTo" id="hotel">
            <h3>Information about the hotel</h3>
            <p>{{some long long text and content}}</p>
        </section>
    </div>
</div>

学分:

Angular相关问答推荐

Angular - MSAL用户缺少角色

导入浏览器模块时出错已导入commonModule的insted,animationBrower也显示错误,当p打开时

第15角Cookie的单元测试用例

CDK虚拟滚动由于组件具有注入属性而在滚动时看到错误的值

如何从URL中角下载图片?

当包含react 性模板绑定时,NG-Bootstrap手风琴不会打开

P-DropDown不会使用react 式表单手动设置Value

Angular 16:信号更新未能更新视图

如果observableA在1秒前触发,则过滤掉observableB

从组件调用时服务中的 AddTodo 方法不起作用

VSCode / Angular 应用程序未完全启动

如何在 Angular 中将tailwind 类传递给 fontawesome

不再需要 core-js 了吗?

如何在 ngFor 循环中创建变量?

取消订阅服务中的http observable

如何在 Angular 4 中使用 Material Design 图标?

Angular将 Select 值转换为 int

测试一个包含 setTimeout() 的函数

如何处理Angular2中的查询参数

如何使用无头浏览器运行测试?