1

を使用してElement.scrollIntoView()います。スクロールは機能していますが、同時にページ全体のレイアウトが崩れます。以下はデモです:

* {
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
}

.content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: lightgray;
}

ul {
  position: absolute;
  width: 100%;
  height: 300px;
  left: 0;
  bottom: 0;
  background-color: gray;
  overflow-y: scroll;
}

li {
  list-style: none;
}

#div {
  position: absolute;
  width: 100%;
  height: 300px;
  left: 0;
  bottom: -300px;
  background-color: darkgray;
}

button {
  position: absolute;
  right: 0;
  bottom: 0;
}
<div class=content>
  <!-- it's the ul to scroll, staying at the bottom of the page -->
  <ul>
    <li id=li>abc</li>
    <li>def</li>
  </ul>
  <!-- it's the div hidden under the bottom edge of the page -->
  <div id=div>i should stay hidden.</div>
  <button onclick=li.scrollIntoView();>scrollIntoView</button>
</div>

<div>ページの下端の下にa を隠します。でscrollIntoView1位<li><ul>なると、隠れていた<div>ものが引き出されます。だけでなく<div>、ページのコンテンツ全体を 300px に引き上げます。

<li>のビューにスクロールするだけだと思い<ul>ます。ページ全体のレイアウトに影響するのはなぜですか?

4

1 に答える 1