CSS 属性

CSS scroll-snap-stop属性决定滚动容器是否会滚动过去或坚持到最近的捕捉位置。

属性值

  • nomal- 默认值。滚动容器将滚动经过捕捉点而不停止。

  • always: 该值强制滚动容器在每个捕捉位置停止,即使您快速滚动经过它们也是如此。

适用范围

所有 HTML 元素。

DOM 语法

object.style.scrollSnapStop = "normal|always"; 

CSS scroll-snap-stop: nomal

以下示例演示如何使用scroll-snap-stop:正常属性 -

<html>
<head>
<style>
   .scroll-container {
      display: flex;
      width: 350px;
      height: 200px;
      overflow-x: auto;
      overflow-y: hidden;
      white-space: nowrap;
      scroll-snap-type: x mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      flex: 0 0 auto;
      width: 300px;
      height: 200px;
      scroll-snap-align: center;
      scroll-snap-stop: normal;
   }
   .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-stop: normal</div>
      <div class="scrolling-section2">scroll-snap-stop: normal</div>
      <div class="scrolling-section3">scroll-snap-stop: normal</div>
      <div class="scrolling-section1">scroll-snap-stop: normal</div>
   </div>
</body>
</html> 

CSS scroll-snap-stop: always

下面的例子演示了如何使用scroll-snap-stop:always属性在指定的捕捉点停止滚动 -

<html>
<head>
<style>
   .scroll-container {
      display: flex;
      width: 350px;
      height: 200px;
      overflow-x: auto;
      overflow-y: hidden;
      white-space: nowrap;
      scroll-snap-type: x mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      flex: 0 0 auto;
      width: 300px;
      height: 200px;
      scroll-snap-align: center;
      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);
   }
</style>
</head>
<body>
   <div class="scroll-container">
      <div class="scrolling-section1">scroll-snap-stop: always</div>
      <div class="scrolling-section2">scroll-snap-stop: always</div>
      <div class="scrolling-section3">scroll-snap-stop: always</div>
      <div class="scrolling-section1">scroll-snap-stop: always</div>
   </div>
</body>
</html>