Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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って楽だけど
バニラはちゃんと中身を見てる感がいい。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away