How to get the top position of an element?

前端 未结 4 854
青春惊慌失措
青春惊慌失措 2020-12-24 00:14

Is it possible to get the top position of an element using javascript/jquery ?

The element is a table, if that matters.

相关标签:
4条回答
  • 2020-12-24 00:55

    If you want the position relative to the document then:

    $("#myTable").offset().top;
    

    but often you will want the position relative to the closest positioned parent:

    $("#myTable").position().top;
    
    0 讨论(0)
  • 2020-12-24 01:01
    var top = event.target.offsetTop + 'px';
    

    Parent element top position like we are adding elemnt inside div

    var rect = event.target.offsetParent;
             rect.offsetTop;
    
    0 讨论(0)
  • 2020-12-24 01:10
    $("#myTable").offset().top;
    

    This will give you the computed offset (relative to document) of any object.

    0 讨论(0)
  • 2020-12-24 01:11

    Try: $('#mytable').attr('offsetTop')

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