CSS - scroll-snap-align 属性



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>
广告