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.

HTMLのダイアログ内のスクロールを指定位置に移動する

Posted at

ページ内で指定位置にスクロールさせたいとき以下のようなアンカーを用いるが

HTML

<a href="#sampleId">指定位置へスクロール</a>

<div id="sampleId">内容</div>

ダイアログ内で指定位置にスクロールさせるにはscrollTopを用いて指定位置に移動させる。

HTML

<!--ダイアログ-->
<div id="dialog">
  <!--指定位置へ移動するボタン-->
  <button onclick="jump()">指定位置へスクロール</button>
  <!--スクロール領域-->
  <div id="contents" style="overflow-y:auto;height:520px">
    <!--移動したい内容-->
    <div id="sampleId">内容</div>
  </div>
</div>

Javascript

function jump() {
  //スクロール領域の位置を取得
  var offsetTop = $("#contents").offset().top;
  //移動したい内容の位置を取得
  var top = $("#sampleId").offset().top;
  //スクロールを移動する
  $("#contents").scrollTop(top - offsetTop);
}
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?