angular2-scroll-module
这篇介绍一下,写一个自己的angular2滚动监听插件
目录结构:
/scrollModule:
ztw-scroll.module.ts;
scrollBind.directive.ts;
scroll.directive.ts;
scroll.service.ts;
使用:
({
template:`
<div ztwScrollBind> //ztwScrollBind用于监听body的scroll行为,可以绑定在任何一个组件上。
<div class='block' *ngFor='let block of blocks'
(scrolled)='scrolled($event)' //滚动进入时触发。
(leaved)='leaved($evnet)' //离开时触发。
ztwScrollControl>
</div>
</div>
`
})
export class component{
this.blocks=[1,2,3]
scrolled(e){
if(!e.target) return;
}
leaved(e){
}
}
ztw-scroll.module.ts:
import {NgModule} from '@angular/core';
import {ScrollDirective} from './scroll.directive';
import {ScrollService} from './scroll.service';
import {ScrollBindDirective} from './scrollBind.directive';
@NgModule({
declarations:[
ScrollDirective,
ScrollBindDirective
],
exports:[
ScrollDirective,
ScrollBindDirective
],
providers:[ScrollService]
})
export class ZTWScrollModule{}
在app.module中imports它。
scroll.service.ts:
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class ScrollService{
scrollTop:Subject<number>=new Subject();
constructor(){};
controlNodes=[]; //储存所有的scrollControl
name:string='bb';
getAbsoluteTop(node){ //获得scrollControl的绝对高度。
let top=node.offsetTop;
if(node.offsetParent) top+=this.getAbsoluteTop(node.offsetParent);
return top;
}
}
scroll.directive.ts:
监听body的滚动行为。
import {Directive} from '@angular/core';
import {ScrollService} from './scroll.service';
@Directive({
selector:'[ztwScrollBind]'
})
export class ScrollDirective{
constructor(
private scrollService:ScrollService
){
window.onscroll=function(){
let topNum=document.querySelector('body').scrollTop;
let nodes=scrollService.controlNodes;
if(nodes===[])return;
nodes.forEach(node=>{ //动态重写每个scrollControl的绝对高度。因为每个scrollControl的绝对高度不一定是固定的。
let top=scrollService.getAbsoluteTop(node);
node.ztw_top=top;
node.ztw_bottom=top+node.offsetHeight;
})
scrollService.scrollTop.next(topNum);
}
}
}
scrollBind.directive.ts:
控制每个scrollControl。
import {Directive,Input,Output,EventEmitter,ElementRef} from '@angular/core';
import {ScrollService} from './scroll.service';
@Directive({
selector:'[ztwScrollControl]'
})
export class ScrollBindDirective{
@Input('ScrollBind')node:string;
@Output() scrolled=new EventEmitter;
@Output() leaved=new EventEmitter;
constructor(
private el:ElementRef,
private scrollService:ScrollService
){ }
ngAfterViewInit(){
let node=this.el.nativeElement;
this.scrollService.controlNodes.push(node);
let sendOnece=true,scrolled=false;
let sendObj={target:node};
this.scrollService.scrollTop.subscribe(v=>{ //给scrollControl分配一个订阅。scroll进入时触发一次scrolled,离开时触发一次leaved。
if(!node.ztw_top) return;
if(v>node.ztw_top&&v<node.ztw_bottom){
if(!sendOnece)return ;
this.scrolled.emit(sendObj);
sendOnece=false;
scrolled=true;
}else{
if(!scrolled) return;
this.leaved.emit(sendObj);
scrolled=false;
sendOnece=true;
}
})
}
}
插件分享:
已分享至github:https://github.com/zhantewei2/angular2-module-share
里面还有一个前段时间,纯JS写的文本编辑器。供大家分享及指教。
angular2-scroll-module的更多相关文章
- jQuery右侧悬浮楼层滚动 电梯菜单
http://www.kaiu.net/effectCon.aspx?id=2198 <!doctype html> <html> <head> <meta ...
- 关于angular跳转路由之后不能自动回到顶部的解决方法
Question: angular2 scroll top on router change 当我们在第一个路由滑动到底部当我们点击导航跳转到另一个路由时页面没有回到顶部而是保持上一个路由的滚动位置, ...
- angular2 ng build --prod 报错:Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
调试页面 ng serve 正常 ng build 也正常 ng build --prod 异常:Module not found: Error: Can't resolve './$$_gendir ...
- [angular2]解决安装 angular-cli 报错:Cannot find module 'github-url-from-git'
1.运行:sudo rm -rf /usr/local/lib/node_modules/npm 2.重新安装最新版本的node,最新版本的node已经集成了npm,所以无需另外安装. 3.运行:su ...
- Angular2学习
1.新建项目 2.新建Model public class TodoItem { public int Id { get; set; } public string Key { get; set; } ...
- Angular2.0的项目架构
Angular2.0的项目架构 一.项目服务端app a) Controller控制器 b) Router路由 c) Service服务 d) Public公共样式及脚本和图片等静态资源 e) Vie ...
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数
上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...
- Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数
上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...
随机推荐
- HTML5 云知梦自觉,记录知识 点
第一章(1--3) 文档类型:<!doctype html> 网站代码结构:<html> <head> <meta charset="UTF-8&q ...
- As of Flume 1.4.0, Avro is the default RPC protocol.
Flume 1.8.0 Developer Guide — Apache Flume http://flume.apache.org/FlumeDeveloperGuide.html The remo ...
- wget -d --header
wget -d --header="Host:www.sina.com" http://202.108.33.84 domain differ ip 防止Wget递归下载 假设Ng ...
- Java 多线程通信之多生产者/多消费者
// 以生产和消费烤鸭为例 class Resource { private String name; private int count = 1; // 记录烤鸭的编号 private boolea ...
- Linux入门之运维(1) 系统监控 vmstat top
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- MariaDB备份之XtraBackup
一.XtraBackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtrabd数据库进行热备的工具.特点: (1)备份过程快速.可靠: ...
- python安装virtualenv
pip install virtualenv 为了使用更方便用,安装另外一个,windows下要-win,linux下不用 pip install virtualenvwrapper-win 使用方法 ...
- mysql的-F与master-data理解(一个小型的big-log恢复)
例子: 使用mysqlbin-log恢复,有两种情况,一个是停数据库,一个是不停 在不停数据库的情况下,为了防止新的写入,需要将bin-log切割,然后新的数据会保存在新的bin-log里面 在此之前 ...
- Mac下 javac java 进行编译和运行含有包路径及引入jar包的类
近两天因为刚入职,属于熟悉环境的阶段,研究了下算法(第四版),当不使用IDE工具直接使用终端进行javac 编译带有包的类,然后使用java 会出现如下错误提示: 使用谷歌搜索了很久,终于找到解决的办 ...
- kubernetes --> kube-dns 安装
准备yaml文件: 1.kubedns-cm.yaml # Copyright 2016 The Kubernetes Authors. # # Licensed under the Apache L ...