index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
main {
width: 100vw;
height: 100vh;
max-width: 100%;
display: grid;
place-items: center;
background-color: hsl(0deg, 0%, 5%);
}
ul {
list-style: none;
}
li {
padding: 0.6em;
}
a {
color: white;
font-size: 2em;
}
</style>
<title>CSS 滚动贴合</title>
</head>
<body>
<main>
<ul>
<li>
<a href="./fullscreen-snap-y.html">垂直贴合</a>
</li>
<li><a href="./fullscreen-snap-x.html">水平贴合</a></li>
<li><a href="./fullscreen-snap-proximity.html">近似贴合</a></li>
<li><a href="./fullscreen-snap-y-padding.html">贴合间距</a></li>
<li><a href="./list-scroll-y.html">Ul 列表贴合</a></li>
</ul>
</main>
</body>
</html>
base.css
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
/* 装饰性样式 */
section {
display: grid;
place-items: center;
font-size: 2em;
}
section:nth-child(1) {
background: rgb(229, 213, 255);
}
section:nth-child(2) {
background: rgb(255, 219, 206);
}
section:nth-child(3) {
background: rgb(242, 240, 186);
}
section:nth-child(4) {
background: rgb(196, 244, 175);
}
h1 {
box-shadow: 0 0 24px hsl(0, 0%, 0%, 0.2);
display: grid;
place-items: center;
font-size: 2em;
background: white;
}
ul {
width: 50%;
list-style: none;
height: 240px;
overflow: scroll;
}
li {
padding: 1em;
text-align: center;
font-size: 2em;
}
li:nth-child(2n) {
background: rgb(229, 213, 255);
}
li:nth-child(2n + 1) {
background: rgb(255, 219, 206);
}
垂直贴合 ./fullscreen-snap-y.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="base.css" />
<title>CSS Scroll Snap 垂直</title>
<style>
/* 核心样式 */
main {
/* mandatory:强制的 */
scroll-snap-type: y mandatory;
/* 需要把滚动条设置到直接父容器,
scroll-snap-type 才能生效,
默认是在 body 上,现在是 main 上 */
overflow: scroll;
height: 100vh;
}
section {
width: 100vw;
height: 150vh;
scroll-snap-align: center;
}
</style>
</head>
<body>
<main>
<section>页面内容1</section>
<section>页面内容2</section>
<section>页面内容3</section>
<section>页面内容4</section>
</main>
</body>
</html>
水平贴合 ./fullscreen-snap-x.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="base.css" />
<title>CSS Scroll Snap 水平贴合</title>
<style>
/* 核心样式 */
main {
display: flex;
scroll-snap-type: x mandatory;
overflow: scroll;
height: 100vh;
width: 100vw;
}
section {
width: 100vw;
height: 100vh;
scroll-snap-align: start;
flex-shrink: 0;
}
</style>
</head>
<body>
<main>
<section>页面内容1</section>
<section>页面内容2</section>
<section>页面内容3</section>
<section>页面内容4</section>
</main>
</body>
</html>
贴合间距 ./fullscreen-snap-y-padding.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="base.css" />
<title>CSS Scroll Snap 贴合间距</title>
<style>
/* 核心样式 */
h1 {
height: 80px;
position: sticky;
top: 0;
}
main {
scroll-snap-type: y mandatory;
scroll-padding: 80px;
overflow: scroll;
height: 100vh;
}
section {
width: 100vw;
height: 100vh;
scroll-snap-align: start;
}
</style>
</head>
<body>
<main>
<h1>H1标题</h1>
<section>页面内容1</section>
<section>页面内容2</section>
<section>页面内容3</section>
<section>页面内容4</section>
</main>
</body>
</html>
Ul 列表贴合 ./list-scroll-y.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="base.css" />
<title>CSS Scroll Snap Ul 列表贴合</title>
<style>
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
main {
width: 100vw;
height: 100vh;
display: grid;
place-items: center;
}
/* 核心样式 */
ul {
scroll-snap-type: y mandatory;
}
li {
scroll-snap-align: start;
}
</style>
</head>
<body>
<main>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</main>
</body>
</html>