--- title: scroll-snap-coordinate slug: Web/CSS/scroll-snap-coordinate tags: - CSS - css snap translation_of: Web/CSS/scroll-snap-coordinate ---
scroll-snap-coordinate
CSS属性结合元素的最近的祖先元素滚动容器的{{cssxref("scroll-snap-destination")}} 定义的轴,定义了元素中x和y坐标偏移的位置。
如果元素已经变型,snap坐标也以相同的方式进行变型,为了使元素的snap点向元素一样被显示。
/* 关键值 */ scroll-snap-coordinate: none; /* <位置>值 */ scroll-snap-coordinate: 50px 50px; /* 单坐标 */ scroll-snap-coordinate: 100px 100px, 100px bottom; /* 多坐标 */ /* 全局值 */ scroll-snap-coordinate: inherit; scroll-snap-coordinate: initial; scroll-snap-coordinate: unset;
none
<position>
<div id="container"> <div> <p>At coordinate (0, 0)</p> <div class="scrollContainer coordinate0"> <div>1</div> <div>2</div> <div>3</div> </div> </div> <div> <p>At coordinate (25, 0)</p> <div class="scrollContainer coordinate25"> <div>1</div> <div>2</div> <div>3</div> </div> </div> <div> <p>At coordinate (50, 0)</p> <div class="scrollContainer coordinate50"> <div>1</div> <div>2</div> <div>3</div> </div> </div> </div>
#container { display: flex; } #container > div:nth-child(-n+2) { margin-right: 20px; } .scrollContainer { width: 100px; overflow: auto; white-space: nowrap; scroll-snap-type: mandatory; font-size: 0; } .scrollContainer > div { width: 100px; height: 100px; display: inline-block; line-height: 100px; text-align: center; font-size: 50px; } .coordinate0 > div { scroll-snap-coordinate: 0 0; } .coordinate25 > div { scroll-snap-coordinate: 25px 0; } .coordinate50 > div { scroll-snap-coordinate: 50px 0; } .scrollContainer > div:nth-child(even) { background-color: #87ea87; } .scrollContainer > div:nth-child(odd) { background-color: #87ccea; }
{{EmbedLiveSample("Example", "100%", "170")}}
规范名称 | 状态 | 评论 |
---|---|---|
{{SpecName("CSS Scroll Snap Points", "#propdef-scroll-snap-coordinate", "scroll-snap-coordinate")}} | {{Spec2("CSS Scroll Snap Points")}} | Initial definition |
{{CompatibilityTable}}
特性 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
支持度 | {{CompatUnknown}} | {{CompatGeckoDesktop("39.0")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
特性 | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
支持度 | {{CompatNo}} | {{CompatGeckoMobile("39.0") }}[1] | {{CompatGeckoMobile("39.0") }} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
[1] 在Firefox移动版上,从内核Gecko 39开始可用,但是低于 layout.css.vertical-text.enabled
优先级,默认不可用。