# 页面宽度导致在访问搜索/用户/汉堡包组合框时自动滚动到页面顶部（仅限 Chrome）

**URL:** <https://meta.discourse.org/t/width-of-page-causes-an-autoscroll-to-the-top-of-the-page-when-accessing-serach-user-hamburger-comboboxes-chrome-only/207459>\
**Category:** Product Support\
**Created:** [2021 年10 月 28 日 21:31 UTC](https://meta.discourse.org/t/width-of-page-causes-an-autoscroll-to-the-top-of-the-page-when-accessing-serach-user-hamburger-comboboxes-chrome-only/207459 "2021-10-28T21:31:25Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

作者： ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)\
发布日期： [2021 年11 月 5 日 16:48 UTC](https://meta.discourse.org/t/width-of-page-causes-an-autoscroll-to-the-top-of-the-page-when-accessing-serach-user-hamburger-comboboxes-chrome-only/207459/17 "2021-11-05T16:48:14Z")

</div>

我今天在桌面版上更新了 Chrome，随后出现了此问题。这应该能解决该问题。

> <https://github.com/discourse/discourse/pull/14822>
>
> Context:
> 
> https://meta.discourse.org/t/width-of-page-causes-an-autoscroll-to-t…he-top-of-the-page-when-accessing-serach-user-hamburger-comboboxes-chrome-only/207459
> 
> When one of the header panels is opened, there's some logic to set the focus either on the first link - in the case of the hamburger and user menus - or the search bar in the case of the search panel.
> 
> Hamburger
> 
> https://github.com/discourse/discourse/blob/1f8939c0f130892397fb7ee8b0f348f2a7f8b636/app/assets/javascripts/discourse/app/widgets/header.js#L497
> 
> User menu 
> 
> https://github.com/discourse/discourse/blob/1f8939c0f130892397fb7ee8b0f348f2a7f8b636/app/assets/javascripts/discourse/app/widgets/header.js#L487
> 
> Search panel
> 
> https://github.com/discourse/discourse/blob/1f8939c0f130892397fb7ee8b0f348f2a7f8b636/app/assets/javascripts/discourse/app/widgets/header.js#L627
> 
> When \`focus()\` is called, the browser automatically scrolls to that element. This wouldn't normally be an issue; however, since the header is set to sticky, it causes problems on Chrome specifically. Here's what it looks like
> 
> https://global.discourse-cdn.com/meta/original/3X/1/e/1e5cafe12de556ba09f13c42cd74f1f38c873868.mp4
> 
> and it's even more prominent on topic pages since it tries to find the top of the page, causing more posts to load, which causes the height to change, which causes jitter.
> 
> This PR adds the \[preventScroll\](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus#focus\_with\_focusoption) option to \`focus()\` to prevent this automatic scrolling
> 
> Here's what it looks like after
> 
> https://global.discourse-cdn.com/meta/original/3X/8/e/8e475f7b3a4cd7151fa034343e88c4ab4d09cb11.mp4

PR 描述中有更多详细信息。该修复现已在 Meta 上线。

---

_[View the full topic](https://meta.discourse.org/t/width-of-page-causes-an-autoscroll-to-the-top-of-the-page-when-accessing-serach-user-hamburger-comboboxes-chrome-only/207459)._
