CSS scroll-snap-align 属性指定捕捉的元素如何在其捕捉容器中定位。它有两个值,第一个用于块轴,第二个用于内联轴。
属性值
none: 捕捉的值元素未与捕捉容器的捕捉端口对齐。
start: 捕捉元素的捕捉点与捕捉容器的捕捉端口的开头对齐。
end: 捕捉元素的捕捉点与捕捉容器捕捉端口的末端对齐。
center: 捕捉元素的捕捉点与捕捉容器的捕捉端口的中心对齐。
捕捉端口是滚动容器中捕捉区域对齐的区域。
适用范围
所有 HTML 元素。
DOM 语法
object.style.scrollSnapAlign = "none|start|end|center";
CSS scroll-snap-align:none
下面的例子演示了如何使用scroll-snap-align: none属性 -
<html>
<head>
<style>
.scroll-container {
display: flex;
width: 350px;
height: 200px;
overflow-x: auto;
overflow-y: hidden;
}
.scrolling-section1,
.scrolling-section2,
.scrolling-section3
{
flex: 0 0 auto;
width: 300px;
height: 200px;
scroll-snap-align: none;
}
.scrolling-section1 {
background-color: rgb(220, 235, 153);
}
.scrolling-section2 {
background-color: rgb(230, 173, 218);
}
.scrolling-section3 {
background-color: rgb(176, 229, 238);
}
</style>
</head>
<body>
<div class="scroll-container">
<div class="scrolling-section1">scroll-snap-align: none</div>
<div class="scrolling-section2">scroll-snap-align: none</div>
<div class="scrolling-section3">scroll-snap-align: none</div>
</div>
</body>
</html>
CSS scroll-snap-align:start
下面的例子演示了如何使用scroll-snap-align: start 属性 -
<html>
<head>
<style>
.scroll-container {
display: flex;
width: 350px;
height: 200px;
overflow-x: auto;
overflow-y: hidden;
scroll-snap-type: x mandatory;
}
.scrolling-section1,
.scrolling-section2,
.scrolling-section3 {
flex: 0 0 auto;
width: 300px;
height: 200px;
scroll-snap-stop: always;
}
.scrolling-section1 {
background-color: rgb(220, 235, 153);
scroll-snap-align: start;
}
.scrolling-section2 {
background-color: rgb(230, 173, 218);
}
.scrolling-section3 {
background-color: rgb(176, 229, 238);
}
</style>
</head>
<body>
<div class="scroll-container">
<div class="scrolling-section1">scroll-snap-align: start</div>
<div class="scrolling-section2">scroll-snap-align</div>
<div class="scrolling-section3">scroll-snap-align</div>
</div>
</body>
</html>
CSS scroll-snap-align: center
以下示例演示如何使用scroll-snap-align: center 属性 -
<html>
<head>
<style>
.scroll-container {
display: flex;
width: 350px;
height: 200px;
overflow-x: auto;
overflow-y: hidden;
scroll-snap-type: x mandatory;
}
.scrolling-section1,
.scrolling-section2,
.scrolling-section3 {
flex: 0 0 auto;
width: 300px;
height: 200px;
scroll-snap-stop: always;
}
.scrolling-section1 {
background-color: rgb(220, 235, 153);
}
.scrolling-section2 {
background-color: rgb(230, 173, 218);
scroll-snap-align: center;
}
.scrolling-section3 {
background-color: rgb(176, 229, 238);
}
</style>
</head>
<body>
<div class="scroll-container">
<div class="scrolling-section1">scroll-snap-align</div>
<div class="scrolling-section2">scroll-snap-align: center</div>
<div class="scrolling-section3">scroll-snap-align</div>
</div>
</body>
</html>
CSS scroll-snap-align: end
以下示例演示如何使用scroll-snap-align: end 属性 -
<html>
<head>
<style>
.scroll-container {
display: flex;
width: 350px;
height: 200px;
overflow-x: auto;
overflow-y: hidden;
scroll-snap-type: x mandatory;
}
.scrolling-section1,
.scrolling-section2,
.scrolling-section3
{
flex: 0 0 auto;
width: 300px;
height: 200px;
scroll-snap-stop: always;
}
.scrolling-section1 {
background-color: rgb(220, 235, 153);
}
.scrolling-section2 {
background-color: rgb(230, 173, 218);
}
.scrolling-section3 {
background-color: rgb(176, 229, 238);
scroll-snap-align: end;
}
</style>
</head>
<body>
<div class="scroll-container">
<div class="scrolling-section1">scroll-snap-align</div>
<div class="scrolling-section2">scroll-snap-align</div>
<div class="scrolling-section3">scroll-snap-align: end</div>
</div>
</body>
</html>