简体   繁体   English

滚动10像素顶部到特定的div

[英]Scroll 10 px top to particular div

I have written below code to scroll top to particular div and it is working fine. 我写了下面的代码来滚动到特定的div,它工作正常。 But I want to scroll 10px more on the particular fields. 但我想在特定字段上再滚动10px。 On click of submit button it is scrolling to the error block, but complete filed is not visible. 单击提交按钮后,它会滚动到错误块,但看不到完整字段。 How do we scroll more to the targeted class. 我们如何更多地滚动到目标类别。

public scrollIntoError(formId: string): void {
  setTimeout(() => {
    const selector = "#" + formId + " .error-class";
    const firstElementWithError = document.querySelector(selector);
    this.scrollToError(firstElementWithError);
  }, NumberConstants.HUNDRED);
} 

public scrollToError(el: Element): void {
  if (el) {
    el.scrollIntoView({behavior: "smooth"});
  }
} 

Above code is working fine but I want to scroll top 10 px more on the selected class. 上面的代码工作正常,但我想在所选类上再滚动显示前10像素。

Edit 编辑

Page dosent have any scroll bar only the middle section which has forms has scroll. 页面没有滚动条,只有具有表单的中间部分才滚动。

This will scroll smooth in once: 这将平滑滚动一次:

public scrollToError(el: Element): void {
    if (el) {
        el.scroll({
            top: el.scrollTop - 10, 
            left: 0,
            behavior: 'smooth'
        });
    }
} 

You can execute one more statement for that: 您可以为此执行另一条语句:

public scrollToError(el: Element): void {
  if (el) {
    el.scrollIntoView({behavior: "smooth"});
    el.scrollBy(0, 10);
  }
} 

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

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