1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > angular7监听滚动条滚动 路由跳转到顶部

angular7监听滚动条滚动 路由跳转到顶部

时间:2023-05-10 02:24:57

相关推荐

angular7监听滚动条滚动 路由跳转到顶部

监听滚动条滚动

ponent.ts

import { fromEvent } from 'rxjs';export class AppComponent implements OnInit {subscribeScoll: any;scrollDis: any = {_top: 0};constructor(private router: Router) {}ngOnInit() {// 监听滚动条滚动this.subscribeScoll = fromEvent(window, 'scroll').subscribe((event) => {this.onWindowScroll(); // 调用});}// 获取滚动条滚动的位置scollPostion() {let t: any;let l: any;let w: any;let h: any;if (document.documentElement && document.documentElement.scrollTop) {t = document.documentElement.scrollTop;l = document.documentElement.scrollLeft;w = document.documentElement.scrollWidth;h = document.documentElement.scrollHeight;} else if (document.body) {t = document.body.scrollTop;l = document.body.scrollLeft;w = document.body.scrollWidth;h = document.body.scrollHeight;}return {top: t,left: l,width: w,height: h};}// 获取不同方向上滚动的距离onWindowScroll() {this.scrollDis._top = this.scollPostion().top;console.log(this.scrollDis._top);}}

路由跳转页面滚动到顶部

ponent.ts

import { Component, OnInit } from '@angular/core';import { Router } from '@angular/router';@Component({selector: 'app-root',templateUrl: './ponent.html',styleUrls: ['./ponent.scss']})export class AppComponent implements OnInit {constructor(private router: Router) {}ngOnInit() {this.router.events.subscribe((event) => {document.documentElement.scrollTop = 0;document.body.scrollTop = 0;});}}

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