How do I get the offset().top value of an element without using jQuery?

前端 未结 6 989
Happy的楠姐
Happy的楠姐 2020-12-01 06:05

I\'m programming a single-page application using the Angular framework. I\'m new to it. I\'ve read this guide to help me understand the fundamental differences between jQuer

6条回答
  •  没有蜡笔的小新
    2020-12-01 06:33

    For Angular 2+ to get the offset of the current element (this.el.nativeElement is equvalent of $(this) in jquery):

    export class MyComponent implements  OnInit {
    
    constructor(private el: ElementRef) {}
    
        ngOnInit() {
          //This is the important line you can use in your function in the code
          //-------------------------------------------------------------------------- 
            let offset = this.el.nativeElement.getBoundingClientRect().top;
          //--------------------------------------------------------------------------    
    
            console.log(offset);
        }
    
    }
    

提交回复
热议问题