CSS - scroll-padding-top



CSS scroll-padding-top 属性指定元素滚动捕捉区域的顶部偏移量。

滚动捕捉区域是滚动容器中元素在用户滚动时将捕捉到的区域。

可能的值

  • <length> − 长度值是一个数值,例如 px、em、百分比。

应用于

所有 HTML 元素。

DOM 语法

object.style.scrollPaddingTop = "length";

CSS 滚动填充顶部 - 零值

以下示例演示如何使用scroll-padding-top属性确保容器顶部没有填充:

<html>
<head>
<style>
   .scroll-container {
      width: 350px;
      height: 200px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
      scroll-padding-top: 0;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      width: 350px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
   }
</style>
</head>
<body>
   <h3>Scroll the content using the scrollbar arrows to see the effect.</h3>
   <div class="scroll-container">
      <div class="scrolling-section1">scroll-padding-top: 0</div>
      <div class="scrolling-section2">scroll-padding-top: 0</div>
      <div class="scrolling-section3">scroll-padding-top: 0</div>
   </div>
</body>
</html>

CSS 滚动填充顶部 - 长度值

以下示例演示如何使用scroll-padding-top属性在滚动容器顶部添加额外的填充:

<html>
<head>
<style>
   .scroll-container {
      width: 350px;
      height: 200px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
      scroll-padding-top: 25px;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      width: 350px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
   }
</style>
</head>
<body>
   <h3>Scroll the content using the scrollbar arrows to see the effect.</h3>
   <div class="scroll-container">
      <div class="scrolling-section1">scroll-padding-top: 25px</div>
      <div class="scrolling-section2">scroll-padding-top: 25px</div>
      <div class="scrolling-section3">scroll-padding-top: 25px</div>
   </div>
</body>
</html>
广告