scroll-snap-points-x

Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它,但也许已从相关的 web 标准中移除,也许正准备移除或出于兼容性而保留。请尽量不要使用该特性,并更新现有的代码;参见本页面底部的兼容性表格以指导你作出决定。请注意,该特性随时可能无法正常工作。

摘要

scroll-snap-points-x CSS属性定义滚动容器中内容的 snap 点的水平位置。

初始值none
适用元素scroll containers
是否是继承属性
Percentagesrelative to same axis of the padding-box of the scroll container
计算值as specified, but with relative lengths converted into absolute lengths
Animation typediscrete

语法

/* 关键值 */
scroll-snap-points-x: none;

/* 重复的 snap 点 */
scroll-snap-points-x: repeat(400px);

/* 全局值 */
scroll-snap-points-x: inherit;
scroll-snap-points-x: initial;
scroll-snap-points-x: unset;

取值

none

滚动容器没有定义任何 snap 点。Elements within the scroll container may still define snap points of behalf of the scroll container.(i don't understand this statement)

repeat(<长度>)

定义 snap 点的一个间隔,从容器相关的边缘开始。仅允许正数长度。

形式语法

Error: could not find syntax for this item

示例

HTML

<div id="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

CSS

#container {
  width: 200px;
  overflow: auto;
  white-space: nowrap;
  scroll-snap-points-x: repeat(100%);
  scroll-snap-type: mandatory;
  font-size: 0;
}

#container > div {
  width: 200px;
  height: 200px;
  display: inline-block;
  line-height: 200px;
  text-align: center;
  font-size: 100px;
}

#container > div:nth-child(even) {
  background-color: #87ea87;
}

#container > div:nth-child(odd) {
  background-color: #87ccea;
}

规范

此属性曾定义在early draft of CSS Scroll Snap Points Module中,但是后来在in favor of element-based snapping规范中被移除

浏览器兼容性

BCD tables only load in the browser