简体   繁体   English

scrollIntoView()向上移动整个页面布局

[英]scrollIntoView() moves the whole page layout up

The whole page layout moves up when we use scrollIntoView(true). 当我们使用scrollIntoView(true)时,整个页面布局会向上移动。 For my requirement I need to use true parameter. 对于我的需求,我需要使用true参数。 I need to avoid this move. 我需要避免此举。 I posted a working copy of this code in https://jsfiddle.net/7v4t31p0/ Thank you in advance. 我在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> 

Use this instead of using true, the default value of true is block: start and inline: nearest the body will go up(start). 使用它而不是true时,true的默认值是block: start and inline: nearest主体将上升(开始)的block: start and inline: nearest In your case its your parent element. 在您的情况下,它是您的父元素。

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

Thank you all. 谢谢你们。 I used ScrollTop instead of ScrollIntoView(). 我使用ScrollTop而不是ScrollIntoView()。

Here is my working code demo https://jsfiddle.net/6gu4rkc1/ 这是我的工作代码演示https://jsfiddle.net/6gu4rkc1/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM