25
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【 javascript / jQuery 】offsetTopとoffset().top

Last updated at Posted at 2018-05-27

element.offsetTop

親からの相対的位置。

offsetTop は、その要素の offsetParent ノードの上端からの相対的な位置を返します。

javascript

element.offsetTop;

jQuery

$(element).position().top

offset().top

documentからみた位置。

javascript

const $element = document.getElementById("element");
const rect = $element.getBoundingClientRect();
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;

rect.top + scrollTop; // offset().top;

参考:https://q-az.net/without-jquery-height-width-offset-scrolltop/

jQuery

$(element).offset().top;

jQueryって楽だけど
バニラはちゃんと中身を見てる感がいい。

25
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?