Element.scrollIntoView() 方法讓當(dāng)前的元素滾動(dòng)到瀏覽器窗口的可視區(qū)域內(nèi)
語法
element.scrollIntoView(); // 等同于element.scrollIntoView(true)
element.scrollIntoView(alignToTop); // Boolean型參數(shù)
element.scrollIntoView(scrollIntoViewOptions); // Object型參數(shù)
參數(shù)
alignToTop (Boolean型參數(shù))
1.如果為true,元素的頂端將和其所在滾動(dòng)區(qū)的可視區(qū)域的頂端對(duì)齊。
2.如果為false,元素的底端將和其所在滾動(dòng)區(qū)的可視區(qū)域的底端對(duì)齊。
scrollIntoViewOptions (Object型參數(shù))
{
behavior: "auto" | "instant" | "smooth",
block: "start" | "end",
}
1.如果是一個(gè)boolean,true 相當(dāng)于{block: "start"},false 相當(dāng)于{block: "end"}
2.behavior這個(gè)選項(xiàng)決定頁面是如何滾動(dòng)的,實(shí)測(cè)auto與instant都是瞬間跳到相應(yīng)的位置,而smooth就是有動(dòng)畫的過程
示例
var element = document.getElementById("box");
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({block: "end", behavior: "smooth"});
注意
取決于其它元素的布局情況,此元素可能不會(huì)完全滾動(dòng)到頂端或底端。