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


 以上jquery,下面js

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

 

另一種寫法

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;
  		}
		});
  }
}

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM