0
0

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 3 years have passed since last update.

【CSS】超簡単!! スクロールに追従する要素を作る

Posted at

はじめに

スクロールすると動きに合わせて追従してくる方法の紹介です。

追従自体はfixedでできるのですが、指定の場所で追従が終了して欲しい事があると思います。
JSしたり範囲したりする事で可能ではありますが、この記事では「特定の要素の範囲内で追従が終わる」様にする事で超簡単に実装できる方法を紹介します。

ezgif.com-gif-maker.gif

開発環境

HTML 5

実装

以下の記述で、追従する要素はform内でのみ画面上で追従してくる様になります。

html
<div class="form">
  <div class="form-item">
    追従する要素
  </div>
</div>

CSSの記述はposition: sticky;のみです。

css
.form-item {
  position: sticky;
  top: 0;
}

これで完成です。
非常に簡単に実装できるので、よかったら参考にしてください。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?