简体   繁体   English

设备宽度小于1200像素时,scrollIntoView不起作用

[英]scrollIntoView not working when device width is less than 1200px

I am using Angular 5 and have a table where I create the table rows with an *ngFor and give them an id which is the username. 我正在使用Angular 5并有一个表,在其中创建带有* ngFor的表行,并为它们提供一个ID(即用户名)。

<tbody *ngFor="let User of FullTable; let i = index">
 <tr id='{{User.username}}'>
   <th scope="row" style="vertical-align: middle;">
    {{User.username}}
   </th>
 </tr>
</tbody>

I also have an input field where I listen to changes and want to search the User by his username and scroll to that table row. 我也有一个输入字段,我在其中监听更改,并想按用户名搜索用户并滚动到该表行。

<input class="form-control mr-sm-2" type="text" name="Search"
placeholder="Search" [(ngModel)]="Search" (input)="onSearchChange()">

The onSearchChange function looks like that onSearchChange函数看起来像这样

onSearchChange() {
  const scroll = document.getElementById(this.Search);
  scroll.scrollIntoView(false)
}

This works well when I'm in my browser, but when I go into responsive view and go under 1200px width it stops working. 当我在浏览器中时,此方法效果很好,但是当我进入响应视图并且宽度小于1200px时,它将停止工作。 It still jumps a bit up/down when it finds a User but it doesn't scroll to the User. 找到用户后,它仍然会向上/向下跳跃,但不会滚动到该用户。

Is there something I'm missing? 有什么我想念的吗? Is this normal behavior? 这是正常行为吗? If it is how can I have scrolling to the element when I'm on smaller devices? 如果是这样,当我在较小的设备上时如何滚动到该元素?

I've fixed the issue by scrolling by the offset not by the found element. 我通过滚动偏移量而不是找到的元素来解决此问题。

So first I find the element: 所以首先我找到元素:

const scroll = document.getElementById(this.Search);

and then I calculate offsetTop: 然后我计算offsetTop:

const TopPos = scroll.offsetTop;

I then scroll with the given offset instead. 然后,我用给定的偏移量滚动。

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

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