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的路由 为了编写样式方便,我们这篇 ...
随机推荐
- Android 中 js 和 原生交互
Android中的WebView 中加载的URL 默认是在手机浏览器中加载的,我们可以覆盖这种默认的动作,让网页在WebView中打开.通过设置WebView的WebViewClent 达到这个效果. ...
- python3 基础数据类型
一.基础数据类型分类 python的数据类型主要包括以下几种: 1.数字 2.字符串 3.列表 4.字典 5.元组 6.集合 1.数字 int 数字主要是用于计算用的,使用方法并不多 #bit_len ...
- Django-1版本的路由层、Django的视图层和模板层
一.Django-1版本的路由层(URLconf) URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:我们就是以这种方式告诉Dja ...
- python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36201499 标准库:一些最爱 集合.堆和双端队 ...
- Cocos2dx3.1-Android环境搭建初体验
初玩Cocos2dx,多多包涵. 感觉版本号之间的差异比較大.相对前面的版本号来说.3.X更easy上手.更方便了. 一.安装python.我的python-2.7.3. 配置环境变量 系统变量里:在 ...
- Angular路由参数传递
一.路由时传递参数的方式 1.在查询参数中传递数据 //页面 <a routerLink="/product" [queryParams]="{id:1}" ...
- 关于C# yield 你会使用吗?
假设有这样一个需求:在一个数据源(下面代码arry)中把其中大于4的数据取出来遍历到前台,怎么做?(不使用linq) , , , , , , , , , }; 第一种情况: 不使用yield的情况下 ...
- MySQL 数据类型(Day41)
一.介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的高度,但宽度是可选的. mysql数据类型概览 #1.数字:(默认都是有符号,宽度指的是显示宽度,与存储无关) ...
- python全栈开发从入门到放弃之字典的应用
1.存值 info_dic={'name':'egon','age':18,'sex':'male'} info_dic['job']='IT' #根据key来存值 print(info_dic) 输 ...
- selenium破解数字验证码
搞了半天,总算弄出来了,识别率还可以,普通的数字验证码 from selenium import webdriver from PIL import Image import pytesseract ...