Get the position of a fixed div with jquery

前端 未结 4 2141
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 11:21

I have a div with position \"fixed\" and I want to get the value of its position relative to the whole document while the user scrolls down the page.

So

相关标签:
4条回答
  • 2021-02-07 11:31

    you can use .offset() to get the current coordinates of the element relative to the document whereas .position() to get the current coordinates of an element relative to its offset parent.

    0 讨论(0)
  • 2021-02-07 11:32

    Unless you refresh the page and the scrollbar is at a different position at the moment it initialize.

    0 讨论(0)
  • 2021-02-07 11:35
    ($("div").offset().top - $(document).scrollTop())
    
    0 讨论(0)
  • 2021-02-07 11:39

    .offset() gives you the coordinates relative to the whole document.

    The .offset() method allows us to retrieve the current position of an element relative to the document. Contrast this with .position(), which retrieves the current position relative to the offset parent. When positioning a new element on top of an existing one for global manipulation (in particular, for implementing drag-and-drop), .offset() is the more useful.

    .offset() returns an object containing the properties top and left.

    Note: jQuery does not support getting the offset coordinates of hidden elements or accounting for borders, margins, or padding set on the body element.

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