CSS - scroll-margin-top



CSS scroll-margin-top 屬性指定元素的滾動捕捉區域的頂部外邊距。

可能的值

  • <length> − 長度值是一個數值,例如 px、em。

應用於

所有 HTML 元素。

DOM 語法

object.style.scrollMarginTop = "length";

CSS 滾動頂部外邊距 - 零值

以下示例演示如何使用 scroll-margin-top: 0 來移除滾動區域周圍的頂部外邊距:

<html>
<head>
<style>
   .scroll-container {
      width: 300px;
      height: 200px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      width: 300px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
      scroll-margin-top: 0;
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
      scroll-margin-top: 0;
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
      scroll-margin-top: 0;
   }
</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-margin-top: 0</div>
      <div class="scrolling-section2">scroll-margin-top: 0</div>
      <div class="scrolling-section3">scroll-margin-top: 0</div>
   </div>
</body>
</html>

CSS 滾動頂部外邊距 - 長度值

以下示例演示如何使用 scroll-margin-top 屬性向每個部分新增頂部外邊距,這會影響內容的滾動行為:

<html>
<head>
<style>
   .scroll-container {
      width: 300px;
      height: 200px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      width: 300px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
      scroll-margin-top: 20px;
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
      scroll-margin-top: 2em;
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
      scroll-margin-top: 10px;
   }
</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-margin-top: 20px</div>
      <div class="scrolling-section2">scroll-margin-top: 2em</div>
      <div class="scrolling-section3">scroll-margin-top: 10px</div>
   </div>
</body>
</html>
廣告

© . All rights reserved.