我注意到scrollIntoView自从我上次看后有一些新的选择.
即,block和inline.这两者有什么区别?我猜测 {block: "start"}会将元素的顶部与页面顶部对齐,但我不确定这会有什么不同inline,或者如何同时使用这两个选项?
Ang*_*tis 48
该block选项决定元素在其可滚动祖先的可见区域内垂直对齐的位置:
{block: "start"},元素在其祖先的顶部对齐.{block: "center"},元素在其祖先的中间对齐.{block: "end"},元素在其祖先的底部对齐.{block: "nearest"},元素:
该inline选项决定元素在其可滚动祖先的可见区域内水平对齐的位置:
{inline: "start"},元素在其祖先的左侧对齐.{inline: "center"},元素在其祖先的中心对齐.{inline: "end"},元素在其祖先的右侧对齐.{inline: "nearest"},元素:
二者block并inline可以在同一时间被用来滚动到一个指定的点在一个运动.
请查看以下代码段,了解每个代码的运作方式.
片段:
/* ----- JavaScript ----- */
var buttons = document.querySelectorAll(".btn");
[].forEach.call(buttons, function (button) {
button.onclick = function () {
var where = this.dataset.where.split("-");
document.querySelector("div#a1").scrollIntoView({
behavior: "smooth",
block: where[0],
inline: where[1]
});
};
});Run Code Online (Sandbox Code Playgroud)
/* ----- CSS ----- */
body {
padding: 500px;
width: 2000px;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100;
}
div#a1 {
width: 1000px;
height: 300px;
background: url(//www.w3schools.com/css/trolltunga.jpg);
background-repeat: no-repeat;
}Run Code Online (Sandbox Code Playgroud)
<!----- HTML ----->
<header>
<button class="btn" data-where="start-start">T-L</button>
<button class="btn" data-where="start-center">T-C</button>
<button class="btn" data-where="start-end">T-R</button>
<button class="btn" data-where="center-start">C-L</button>
<button class="btn" data-where="center-center">C-C</button>
<button class="btn" data-where="center-end">C-R</button>
<button class="btn" data-where="end-start">B-L</button>
<button class="btn" data-where="end-center">B-C</button>
<button class="btn" data-where="end-end">B-R</button>
</header>
<div id = "a1"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7494 次 |
| 最近记录: |