Angular 10:带有position absolute和overflow: auto的div上的Router的anchorScrolling不起作用。
创始人
2024-10-15 09:00:58
0

在使用Angular 10中,如果给一个带有position: absoluteoverflow: auto样式的div上的Router的anchorScrolling不起作用,可以尝试以下解决方法:

  1. 确保在使用RouterModule.forRoot()方法配置路由时启用了anchorScrolling选项。示例代码如下:
import { NgModule } from '@angular/core';
import { RouterModule, Routes, Scroll } from '@angular/router';

const routes: Routes = [
  // 路由配置
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {
    anchorScrolling: 'enabled', // 启用anchorScrolling选项
    scrollPositionRestoration: 'enabled',
    scrollOffset: [0, 64], // 如果有固定的导航栏,可以设置偏移量
  })],
  exports: [RouterModule]
})
export class AppRoutingModule { }
  1. 在带有position: absoluteoverflow: auto样式的div上,添加scrollable属性。示例代码如下:
  1. 在组件的构造函数中注入ScrollingService,并调用scrollIntoView()方法手动滚动到指定的锚点。示例代码如下:
import { Component, OnInit, ElementRef } from '@angular/core';
import { ScrollingService } from '@angular/cdk/scrolling';

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponent implements OnInit {

  constructor(private scrollingService: ScrollingService, private elementRef: ElementRef) { }

  ngOnInit(): void {
    const element = this.elementRef.nativeElement.querySelector('#your-anchor-id');
    this.scrollingService.scrollIntoView(element);
  }

}

以上是一些可能的解决方法,根据具体情况选择适合的方式。

相关内容

热门资讯

据公告内容!来玩app破解,宝... 据公告内容!来玩app破解,宝宝浙江辅助工具,好像真的是有辅助脚本(哔哩哔哩)1、进入到宝宝浙江辅助...
有挂透视!xpoker辅助控制... 有挂透视!xpoker辅助控制,we poker免费辅助器(透视)果然一直都是有辅助攻略(哔哩哔哩)...
现有关情况通报如下!aapok... 您好,新众亿卡看底牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
解迷透视!哈糖大菠萝有没有挂,... 解迷透视!哈糖大菠萝有没有挂,约局吧德州可以透视吗(透视)真是有辅助脚本(哔哩哔哩)1、约局吧德州可...
相较于以往!wpk俱乐部是做什... 相较于以往!wpk俱乐部是做什么的,赣牌圈总输,确实真的有辅助脚本(哔哩哔哩)1)赣牌圈总输有没有挂...
了解透视!约局吧app有挂吗,... 了解透视!约局吧app有挂吗,xpoker辅助工具(透视)好像有辅助技巧(哔哩哔哩)1、约局吧app...
刚刚!wepoker公共底牌,... 刚刚!wepoker公共底牌,乐酷有没有挂,其实是真的有辅助神器(哔哩哔哩)乐酷有没有挂是不是有人用...
揭幕透视!pokemmo辅助器... 揭幕透视!pokemmo辅助器脚本下载,菠萝德州透视脚本(透视)原来真的有辅助教程(哔哩哔哩)1、任...
有了最新消息!德普之星怎么设置... 有了最新消息!德普之星怎么设置埋牌,微玩体育辅助器,切实真的是有辅助技巧(哔哩哔哩)进入游戏-大厅左...
揭幕透视!sohoo竞技联盟辅... 揭幕透视!sohoo竞技联盟辅助器,拱趴大菠萝辅助神器(透视)一贯一直总是有辅助app(哔哩哔哩)1...