1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Angular 4_监听滚动条(用来锁定行)

Angular 4_监听滚动条(用来锁定行)

时间:2024-07-16 02:58:20

相关推荐

Angular 4_监听滚动条(用来锁定行)

第一种方法

import { ActivatedRoute } from '@angular/router';import { DOCUMENT } from '@angular/platform-browser';

subscribeScoll: any;ngOnInit(): void {this.columnTop = '0';this.subscribeScoll = Observable.fromEvent(window, 'scroll').debounceTime(50) // 防抖.subscribe((event) => {this.onWindowScroll();});}// 组件销毁时取消订阅事件,防止出现页面多次执行之后卡顿ngOnDestroy() {this.subscribeScoll.unsubscribe();}

onWindowScroll() {this.columnTop = (window.pageYOffset || this.document.documentElement.scrollTop || this.document.body.scrollTop) + 'px';}

<thead><tr><th style="position: relative;" [style.top]="columnTop"><span>Item</span></th><th style="position: relative;" [style.top]="columnTop"><span>Delivery</span></th><th style="position: relative;" [style.top]="columnTop"><span>Material</span></th><th style="position: relative;" [style.top]="columnTop"><span>Description</span></th><th style="position: relative;" [style.top]="columnTop"><span>Picked Qty</span></th><th style="position: relative;" [style.top]="columnTop"><span>UoM</span></th></tr></thead>

第二种方法

HTML:

<div (scroll)="onScroll($event)"></div>

TypeScript:

onScroll(event): void {// Interpret the scroll event// Do stuff on inner div scroll}

也有这样的

export class YourComponent {@HostListener('scroll', ['$event']) private onScroll($event:Event):void {console.log($event.srcElement.scrollLeft, $event.srcElement.scrollTop);};}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。