我使用 this article 作为参考点,特别是 this working snippet ,但在我的页面(下面的脚本)中,垂直对齐滚动不起作用。为什么?
.parent {
height: 100vh;
scroll-snap-type: mandatory;
scroll-snap-points-y: repeat(100vh);
scroll-snap-type: y mandatory;
}
section {
height: 100vh;
scroll-snap-align: start;
position: relative;
}
.one {
background-color: red;
}
.two {
background-color: blue;
}
.three {
background-color: grey;
}
.four {
background-color: green;
}
<div class="parent row">
<section class="one">
</section>
<section class="two">
</section>
<section class="three">
</section>
<section class="four">
</section>
</div>
The original answer 表示将
overflow: hidden
直接放在html
和body
元素上,这会搞砸很多事情,即使用position: sticky
的标题,这是一个非常常见的概念。另一种方法是将
scroll-snap-type
属性放在主体上,然后将scroll-snap-align
属性放在需要具有滚动捕捉行为的元素上。