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

Angular 实现Bootstrap ScrollSpy控件的更多相关文章

  1. 支持Angular 2的表格控件

    前端框架一直这最近几年特别火的一个话题,尤其是Angular 2拥有众多的粉丝.在2016年9月份Angular 2正式发布之后,大量的粉丝的开始投入到了Angular 2的怀抱.当然这其中也包括我. ...

  2. Angular 2的表格控件

    Angular 2的表格控件 前端框架一直这最近几年特别火的一个话题,尤其是Angular 2拥有众多的粉丝.在2016年9月份Angular 2正式发布之后,大量的粉丝的开始投入到了Angular ...

  3. angularjs 整合bootstrap 时间控件

    一.引入js <link href="${basePath}/static/plugin/bootstrap/css/bootstrap-datetimepicker.min.css& ...

  4. bootstrap日期控件(双日期、清空等问题解决)

    bootstrap以它优美的外观和丰富的组件,使它成为目前最流行的前端框架.在项目开发中,我们使用它的日期控件确实遇到了一些问题: 1.日期控件后面两个图标点击触发失效 2.双日期关联问题 3.双日期 ...

  5. Bootstrap 时间控件datetimepicker与timepicker

    一.datetimepicker 首先,我们看看点击选择时间的时候的展示页面吧 年                                                月           ...

  6. js插件---Bootstrap 树控件

    js插件---Bootstrap 树控件 一.总结 一句话总结:可以直接用gojs,或者搜索js,jquery的树控件,或者bootstrap树控件,一大堆 gojs 二.JS组件系列——Bootst ...

  7. bootstrap 有些控件需要调用锚点,会与angular 路由 冲突

    最简单的方法 就是 在 #号前加/, 但有人说 在服务器上回失效,也不知道是什么原理.慎用 最靠谱的方法 就 是 使用bootstrap中的js控制控件, 比如轮播图的上一页 下一页,就可以在 ang ...

  8. [转]通过AngularJS directive对bootstrap日期控件的的简单包装

    本文转自:http://www.cnblogs.com/Benoly/p/4109460.html 最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的 ...

  9. [整理]通过AngularJS directive对bootstrap日期控件的的简单包装

    最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的例子,可以使用AngularJS的directive做简单的处理,这样在html里直接使用申明的的形 ...

随机推荐

  1. SpringCloud服务注册与服务发现之Eureka

    Eureka是SpringCloud Netflix的子模块之一,用于云端的服务发现,服务定位,实现云端中间层服务发现和故障转移.服务注册与发现对于微服务系统来说十分的重要,有了服务注册与发现,就省去 ...

  2. 整理的开源项目(全c#)

    NPOI:读写office办公软件(不需要安装office软件) http://npoi.codeplex.com/downloads/get/70099 消息中间件:DotNetMQ http:// ...

  3. Flask-SQLAlchemy插件

    一,初始化 两种方式: db = SQLAlchemy() def create_app(): app = Flask(__name__) db.init_app(app) return app ap ...

  4. vector源码2(参考STL源码--侯捷):空间分配、push_back

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  5. jieba分词(2)

    结巴分词系统中实现了两种关键词抽取法,一种是TF-IDF关键词抽取算法另一种是TextRank关键词抽取算法,它们都是无监督的算法. 以下是两种算法的使用: #-*- coding:utf-8 -*- ...

  6. ASP.NET Core 中使用 Hangfire 定时启动 Scrapyd 爬虫

    用 Scrapy 做好的爬虫使用 Scrapyd 来管理发布启动等工作,每次手动执行也很繁琐;考虑可以使用 Hangfire 集成在 web 工程里. Scrapyd 中启动爬虫的请求如下: curl ...

  7. android studio 查看包有没有重复引用

    在windows下studio内的命令行里面     gradlew :app:dependencies 苹果系统  ./gradlew :app:dependencies

  8. 【原创】基于Bootstrap的Modal二次封装

    前言 Bootstrap:Twitter推出的一个开源的用于前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架 官方网站: ...

  9. http错误代码含义

    "100" : Continue "101" : witching Protocols "200" : OK "201" ...

  10. 【教程向】——基于hexo+github搭建私人博客

    前言 1.github pages服务生成的全是静态文件,访问速度快: 2.免费方便,不用花一分钱就可以搭建一个自由的个人博客,不需要服务器不需要后台: 3.可以随意绑定自己的域名,不仔细看的话根本看 ...