纯CSS实现全屏滚动效果

实现全屏滚动可以不依赖 JS, 只使用纯 CSS 实现, 不过只有较新版本的浏览器支持这一特性

主要使用了以下 CSS 属性

  • scroll-snap-type: y mandatory; 给父元素设置, 设置贴合类型

  • scroll-snap-align: start; 给子元素设置, 设置贴合点

1
2
3
4
5
6
<main>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</main>

容器布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<style>
main {
width: 100vw;
height: 100vh;
scroll-snap-type: y mandatory;
overflow: scroll;
}
main div {
border: black 2px solid;
scroll-snap-align: start;
width: 100vw;
height: 100vh;
background-color: skyblue;
}
</style>

样式设置

作者

cuicui

发布于

2023-02-13

更新于

2023-04-23

许可协议

评论