CSS scroll-margin-left 属性指定元素滚动捕捉区域的左边距。
属性值
<length>: 长度值是一个数值,例如 px、em。
适用范围
所有 HTML 元素。
DOM 语法
object.style.scrollMarginLeft = "length";
CSS scroll-margin-left - 0
下面的示例演示如何使用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>使用滚动条箭头滚动内容以查看效果。</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>使用滚动条箭头反向滚动内容即可查看效果。</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>