Bootstap是基于JQuery开发,Angular中不支持Bootstrap相关事件逻辑。本文基于Typecript开发了一个Angular可用的ScrollSpy控件。Scrollspy控件主要实现了左侧导航以及右侧正文之间的联动和切换。所以,

  此组件主要解决两个问题:

  (1)点击左侧导航列表,右侧正文能够跟随切换的焦点定位到具体的段落,即添加导航点击事件

  (2)右侧正文滚动时,左侧导航列表可以根据正文滚动的位置自动定位到,该段落所属的目录索引上。即添加正文滚动事件。

  1. 代码结构

1.组件代码

滚动监听指令:

  监听滚动事件,并且获取当前滚动焦点的元素值(element)然后切换当前导航树位置(postion),触发positionchange事件函数。

//scrollspy.direct.ts
import { Directive, Injectable, Input, EventEmitter, Output, ElementRef, HostListener } from '@angular/core'; @Directive({
selector: '[NgScrollSpy]'
})
export class NgScrollSpyDirective { @Output() public positonChange = new EventEmitter<string>();
private currentposition: string;
private Tags = ['DIV']; constructor(private _el: ElementRef) {} @HostListener('scroll', ['$event']) //监听正文滚动事件
onScroll(event: any) {
let position: 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.Tags.some(tag => tag === element.tagName)) {
if ((element.offsetTop - parentOffset) <= scrollTop) {
position = element.id;
}
}
}
if (position !== this.currentposition) {
this.currentposition = position;
this.positonChange.emit(this.currentposition);
}
} }

  通过scrollTo(position :string)函数获取左侧导航树点击位置,并操作DOM定位到对应位置。

  通过onPositionChange(position: string)函数获取右侧正文的滚动位置。

//ngscrollspy.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'app-ngscrollspy',
templateUrl: './ngscrollspy.component.html',
styleUrls: ['./ngscrollspy.component.css']
})
export class NgscrollspyComponent implements OnInit { constructor() { } ngOnInit() {
} currentPostion = 'Phase1'; // 正文滚动事件 设置当前滚动位置,供NgScrollSpyDirective使用
onPositionChange(position: string) {
this.currentPostion = position;
} // 导航点击事件 查找目录节点别切换到节点
scrollTo(position :string) {
document.querySelector('#' + position)
.scrollIntoView();
}
}

html

//ngscrollspy.component.html
<div class="container">
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-6">
<h2>导航</h2>
<div (click)="scrollTo('phase1')">段落1
<ng-container *ngIf="currentPostion==='phase1'">++</ng-container>
</div>
<div (click)="scrollTo('phase2')">段落2
<ng-container *ngIf="currentPostion==='phase2'">++</ng-container>
</div>
<div (click)="scrollTo('phase3')">段落3
<ng-container *ngIf="currentPostion==='phase3'">++</ng-container>
</div>
<div (click)="scrollTo('phase4')">段落4
<ng-container *ngIf="currentPostion==='phase4'">++</ng-container>
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-6">
<div id="phases" NgScrollSpy (positonChange)="onPositionChange($event)" style="height:150px;overflow-y: scroll;">
<div id="phase1">
<h2 style="margin:0">段落 1</h2>
<PhaseText></PhaseText>
</div>
<div id="phase2">
<h1>段落 2</h1>
<PhaseText></PhaseText>
</div>
<div id="phase3">
<h1>段落 3</h1>
<PhaseText></PhaseText>
</div>
<div id="phase4">
<h1>段落 4</h1>
<PhaseText></PhaseText>
</div>
</div>
</div>
</div>
</div>

详细代码可以访问github仓库

https://github.com/OshimaYong/AngularDemo

最新文章

  1. Python自动获取数据库表结构
  2. java实现调用ORACLE中的游标和包
  3. js校验输入字符串的字节长度
  4. eclipse技巧总结
  5. mac 下 xampp 多域名 多站点 多虚拟主机 配置
  6. python中x的平方
  7. CentOS 6.3 NFS的安装配置、启动及mount挂载方法
  8. MAC 环境 ionic build android 命令在&quot;Downloading http://services.gradle.org/distributions/gradle-2.13-all.zip&quot;卡住问题
  9. HC-05蓝牙模块基本使用
  10. Temporary failure in name resolution
  11. hihocoder第42周 k*N骨牌覆盖(状态dp+矩阵快速幂)
  12. 大数据Hadoop学习之了解Hadoop(1)
  13. zabbix使用SNMPV3协议监控交换机
  14. ASP.NET Core IHostEnvironment和IApplicationLifetime介绍
  15. 1131 Subway Map DFS解法 BFS回溯!
  16. spark Pair RDD 基础操作
  17. IOS 缓存方案(按需缓存 、 预缓存)及 低网速模拟
  18. 有人问thinkphp的标签解析的时候为什么出现标签内内容空格丢失
  19. HDU 1251 统计难题 (Trie)
  20. slice切割数组arr=[[0,1],[2,3]]

热门文章

  1. [HTML] 动态修改input placeholder的颜色
  2. js获取当前页面相关信息
  3. 遗忘Windows Server 2008R2密码的处理方法
  4. Java java.lang.ExceptionInInitializerError 错误解决方案
  5. nginx在centos 7中源码编译安装【添加grpc的支持】
  6. Java读取文件加锁代码Demo(利用Java的NIO)
  7. vector源码2(参考STL源码--侯捷):空间分配、push_back
  8. Mysql压缩包版的安装方法详解
  9. Spring学习--Spring事务相关速记
  10. 【Java初探外篇02】——关于静态方法与实例方法