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. 01-Linux的基本指令

    Linux的基本指令 基础指令(重点) 1.ls指令: 含义:ls(list) 用法1:#ls 含义:列出当前工作目录下的所有文件/文件夹的名称 用法2:#ls  路径 含义:列出指定路径下的所有文件 ...

  2. odoo 默认显示字段

    @api.multi def generate_customs_declaration(self): # if len(self.mapped('cus_goods_list_ids')) != 1: ...

  3. Thrift 基础(C++ rpc )

    一.thrift简介 thrift是Facebook开源的一套rpc框架,目前被许多公司使用 我理解的特点 使用IDL语言生成多语言的实现代码,程序员只需要实现自己的业务逻辑 支持序列化和反序列化操作 ...

  4. C#集成FastDFS断点续传

    C#集成FastDFS断点续传 参考 .net版本FastDFS客户端v5.05. https://github.com/zhouyh362329/fastdfs.client.net FastDFS ...

  5. Azure Redis 缓存使用注意事项与排查问题文档整理

    StackExchange.Redis 使用名为 synctimeout 的配置设置进行同步操作,该设置的默认值为 1000 毫秒. 如果同步调用未在规定时间内完成,StackExchange.Red ...

  6. 全网最详细的Hadoop HA集群启动后,两个namenode都是standby的解决办法(图文详解)

    不多说,直接上干货! 解决办法 因为,如下,我的Hadoop HA集群. 1.首先在hdfs-site.xml中添加下面的参数,该参数的值默认为false: <property> < ...

  7. js获取上一个兄弟元素

    需要用到的两个属性:previousSbiling和previousElementSibling previousSibling:获取元素的上一个兄弟节点:(既包含元素节点.文本节点.注释节点) pr ...

  8. 最大子数组问题/Maximum Subarray

    问题描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  9. 【工具向01】——markdown 文本编辑语言相关

    markdown简介 Markdown是一种轻量级标记语言创始人为约翰·格鲁伯.它允许人们"使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML或HTML文档".这种语言吸 ...

  10. codis测试

    .通过客户端连接codis-proxy来进行测试 codis-proxy是所有redis操作的代理对象,其连接的端口为19000 $ cd /usr/local/codis/bin $ ./redis ...