解決angular單頁面頁面底部跳轉到新頁面滾動條不在頂部的問題

 以上jquery,下面jsjavascript

this.router.events.subscribe((event) => {
	document.body.scrollTop=0;
});

 

另外一種寫法css

import { Component, ViewEncapsulation, OnInit } from '@angular/core';
import { Router, ActivationEnd, NavigationEnd } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less','../assets/css/style.less'],
  encapsulation: ViewEncapsulation.None //Emulated當前  None全局
})
export class AppComponent {
  title = '數據平臺';
  constructor(private  router: Router){
  	
  }
  ngOnInit(){
  	 this.router.events.subscribe((event:NavigationEnd) => {
  		if (event instanceof ActivationEnd){// 當導航成功結束時執行
	    	document.documentElement.scrollTop = 0;
	    	//document.body.scrollTop.scrollTop = 0;
  		}
		});
  }
}
相關文章
相關標籤/搜索