scrollIntoView(),滚动到指定区域

滚动到指定区域

Element.scrollIntoView() 方法让当前的元素滚动到浏览器窗口的可视区域内。

element.scrollIntoView(); // 等同于element.scrollIntoView(true)
element.scrollIntoView(alignToTop); // Boolean型参数
element.scrollIntoView(scrollIntoViewOptions); // Object型参数

参数
alignToTop: 一个boolean值

  • true:等价于 scrollIntoViewOptions: {block: “start”, inline: “nearest”}
  • false:等价于scrollIntoViewOptions: {block: “end”, inline: “nearest”}

scrollIntoViewOptions: 对象

  • behavior: 定义动画过度效果, ‘auto / smooth’ , 默认 ‘auto’
  • block:定义垂直方向的对齐, “start / center / end / nearest”。默认为 “start”。
  • inline 定义水平方向的对齐, “start / center / end / nearest”。默认为 “nearest”

使用方法

scrollIntoView(id: string) {
    let element = document.getElementById(id);
    if (element) {
      element.scrollIntoView();
    }
  }

你可能感兴趣的:(js)