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

jQueryでスクロールダウンを導入してみた

Posted at

ボタンを押した際に同じページ内で指定の場所にスクロールするやり方を実装してみました!

スクロールダウンの方法

今回はid名buttonを押した際にid名header部分にスクロールするように指定してみる

//変数を指定する
var header = $('#header').offset().top;

//id名buttonをクリックした際の関数を指定する
$('#button').click(function(){
  $('html,body').animate({
  {
    scrollTop: header
 }
)};

解説

.offset()ってなんぞや?となると思うので、自分はなりました笑
.offsetとは指定した要素のドキュメントの左上からのy軸方向の距離を取得する。
つまり今回の例で言うとid名headerの位置をドキュメントから見てのy軸の距離を取得したという事になります。そしてその取得した値を変数headerに代入したと言う事になります。

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?