LoginSignup
1
2

More than 5 years have passed since last update.

jQueryで要素が画面内に存在しているかどうか調べる

Posted at
scrollTop = $(window).scrollTop();
scrollBottom = scrollTop + $(window).height();
if ($target.offset().top > scrollTop && $target.offset().top < scrollBottom) {
  // 存在するとき
}
else {
  // 存在しないとき
}

position: fixed な要素があれば、その高さを考慮するほうがいいかも。また、上のコードは「要素の上端が画面内に存在するとき」になっているので、対象が高さのある要素だったりすると適宜修正が必要かも。

1
2
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
1
2