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的路由 为了编写样式方便,我们这篇 ...
随机推荐
- 巨蟒python全栈开发-第13天 内置函数 匿名函数lambda
一.今日内容总览 1.内置函数(1):并不是每一个内置函数都是那么常用 上菜:内置函数部分//思维导图:https://www.processon.com/view/link/5b4ee15be4b0 ...
- Spoken English Practice( Believe it or not, I don't need to make believe its a big deal. (believe,deal, You don't say))
音标复习 绿色:连读:红色:略读:蓝色:浊化:橙色:弱读 口语蜕变(2017/6/25) Sorry, t ...
- 18.android studio 安装ing
1.首先得FQ,在谷歌中搜索android studio 2.安装时出现的问题. a. 解决方法,重启电脑,进入Bios,找到并将值设置为 :Intel Virtual Technology=Enab ...
- simplest_ffmpeg_grabdesktop:屏幕录制。 simplest_ffmpeg_readcamera:读取摄像头
最简单的基于FFmpeg的AVDevice例子(屏幕录制) - 雷霄骅(leixiaohua1020)的专栏 - CSDN博客 https://blog.csdn.net/leixiaohua1020 ...
- SET NAMES 'charset_name'
设置写入db和db返回读出结果的字符集character set https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html SET ...
- ES6 Promise对象then方法链式调用
then()方法的作用是Promise实例添加解决(fulfillment)和拒绝(rejection)状态的回调函数.then()方法会返回一个新的Promise实例,所以then()方法后面可以继 ...
- Vue中动态添加多个class
vue中可以通过 :class=""这样来根据一定的条件来动态添加class,但是有时候需要判断的条件比较多,需要动态添加的class也比较多,这个时候其实也很简单 先看一下示例: ...
- 【我的Android进阶之旅】 Android Studio插件之Jenkins插件介绍
一Jenkins插件功能介绍 1Jenkins任务列表 2切换Jenkins分组 3构建Jenkins任务 4进入构建Jenkins任务的页面 5进入最后一次构建Jenkins任务的页面 6增加Jen ...
- Tomcat 自定义默认网站目录
上面访问的网址为http://192.168.0.108:8080/memtest/meminfo.jsp 需求: 现在我想访问格式为http://192.168.0.108:8080/meminfo ...
- [转载]有经验的Java开发者和架构师容易犯的10个错误
首先允许我们问一个严肃的问题?为什么Java初学者能够方便的从网上找到相对应的开发建议呢?每当我去网上搜索想要的建议的时候,我总是能发现一大堆是关于基本入门的教程.书籍以及资源.同样也发现网上到处充斥 ...