我在 Angular 中需要与 jQuery/HTML 相同的结果。使用 jQuery,无论是否滚动,我都会得到相同的 topOffset 值。
// get the top offset of the dropdown (distance from top of the page)
var topOffset = $(".dropdown").offset().top;
供您参考,在 Angular 中,我将模板变量与@ViewChild一起使用,但 topOffset 值已更改,并且没有提供与 jQuery 相同的所需结果。
@ViewChild('dp') dropdown: ElementRef
functionName() {
let topOffset = this.dropdown.nativeElement.getBoundingClientRect().top;
}
请在评论中告诉我,我必须做什么才能达到所需的结果。