【问题标题】:scrollIntoView() moves the whole page layout upscrollIntoView() 将整个页面布局向上移动
【发布时间】:2019-07-31 03:13:15
【问题描述】:

当我们使用 scrollIntoView(true) 时,整个页面布局向上移动。对于我的要求,我需要使用 true 参数。我需要避免这个动作。我在https://jsfiddle.net/7v4t31p0/ 中发布了此代码的工作副本,谢谢。

.body {
  display: flex;
  width: 100%;
  border: 1px solid gray;
}

.left {
  flex: 0 0 200px;
}

.right {
  flex: 1 1 auto;
  border: 1px solid green;
}

.details {
  display: flex;
  width: 100%;
}

.links {
  display: flex;
  flex-direction: column;
}

.content {
  max-height: 100px;
  overflow-y: auto;
  flex: 1 1 auto;
}

.content div {
  min-height: 100px;
}
<div>
  <div>
    Header
  </div>
  <div class="body">
    <div class="left">
      Left panel
    </div>
    <div class="right">
      <div class="details">
        <div class="links">
          <button id='btn1'>link 1</button>
          <button id='btn2'>link 2</button>
          <button id='btn3'>link 3</button>
        </div>
        <div class="content">
          <div id="content1"> Content 1</div>
          <div id="content2"> Content 2</div>
          <div id="content3"> Content 3</div>
        </div>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

标签: javascript html css


【解决方案1】:

使用这个而不是使用true,true的默认值为block: start and inline: nearest body会上升(开始)。在您的情况下,它是您的父元素。

yourContent.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' })

【讨论】:

    【解决方案2】:

    谢谢大家。我使用 ScrollTop 而不是 ScrollIntoView()。

    这是我的工作代码演示 https://jsfiddle.net/6gu4rkc1/

    content1.parentNode.scrollTop = content1.offsetTop - 30;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多