CSS - scroll-margin-left



CSS scroll-margin-left 屬性指定元素滾動捕捉區域的左邊距。

可能的值

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

應用於

所有 HTML 元素。

DOM 語法

object.style.scrollMarginLeft = "length";

CSS Scroll Margin Left - 零值

下面的例子演示瞭如何使用 scroll-margin-left: 0,這會導致在節的左側沒有設定邊距:

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

CSS Scroll Margin Left - 長度值

下面的例子演示瞭如何使用 scroll-margin-left 屬性向每個節新增左邊距,這會在向後滾動時影響滾動行為:

<html>
<head>
<style>
   .scroll-container {
      display: flex;
      width: 300px;
      height: 200px;
      overflow-x: scroll;
      overflow-y: hidden;
      scroll-snap-type: x mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      flex: 0 0 auto;
      width: 300px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
      scroll-margin-left: 2em;
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
      scroll-margin-left: 10px;
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
      scroll-margin-left: 20px;
   }
</style>
</head>
<body>
   <h3>Scroll the content reversely using the scrollbar arrows to see the effect.</h3>
   <div class="scroll-container">
      <div class="scrolling-section1">scroll-margin-left: 2em</div>
      <div class="scrolling-section2">scroll-margin-left: 10px</div>
      <div class="scrolling-section3">scroll-margin-left: 20px</div>
   </div>
</body>
</html>
廣告