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

前端 未结 6 980
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

    Seems you can just use the prop method on the angular element:

    var top = $el.prop('offsetTop');
    

    Works for me. Does anyone know any downside to this?

    0 讨论(0)
  • 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);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 06:36

    use getBoundingClientRect if $el is the actual DOM object:

    var top = $el.getBoundingClientRect().top;
    

    JSFiddle

    Fiddle will show that this will get the same value that jquery's offset top will give you

    Edit: as mentioned in comments this does not account for scrolled content, below is the code that jQuery uses

    https://github.com/jquery/jquery/blob/master/src/offset.js (5/13/2015)

    offset: function( options ) {
        //...
    
        var docElem, win, rect, doc,
            elem = this[ 0 ];
    
        if ( !elem ) {
            return;
        }
    
        rect = elem.getBoundingClientRect();
    
        // Make sure element is not hidden (display: none) or disconnected
        if ( rect.width || rect.height || elem.getClientRects().length ) {
            doc = elem.ownerDocument;
            win = getWindow( doc );
            docElem = doc.documentElement;
    
            return {
                top: rect.top + win.pageYOffset - docElem.clientTop,
                left: rect.left + win.pageXOffset - docElem.clientLeft
            };
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:38

    Here is a function that will do it without jQuery:

    function getElementOffset(element)
    {
        var de = document.documentElement;
        var box = element.getBoundingClientRect();
        var top = box.top + window.pageYOffset - de.clientTop;
        var left = box.left + window.pageXOffset - de.clientLeft;
        return { top: top, left: left };
    }
    
    0 讨论(0)
  • 2020-12-01 06:49

    the accepted solution by Patrick Evans doesn't take scrolling into account. i've slightly changed his jsfiddle to demonstrate this:

    css: add some random height to make sure we got some space to scroll

    body{height:3000px;} 
    

    js: set some scroll position

    jQuery(window).scrollTop(100);
    

    as a result the two reported values differ now: http://jsfiddle.net/sNLMe/66/

    UPDATE Feb. 14 2015

    there is a pull request for jqLite waiting, including its own offset method (taking care of current scroll position). have a look at the source in case you want to implement it yourself: https://github.com/angular/angular.js/pull/3799/files

    0 讨论(0)
  • 2020-12-01 06:54

    You can try element[0].scrollTop, in my opinion this solution is faster.

    Here you have bigger example - http://cvmlrobotics.blogspot.de/2013/03/angularjs-get-element-offset-position.html

    0 讨论(0)
提交回复
热议问题