scroll-snap-destination - CSS: Cascading Style Sheets 编辑
Obsolete
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
The scroll-snap-destination
CSS property defines the position in x and y coordinates within the scroll container's visual viewport which element snap points align with.
/* <position> value */
scroll-snap-destination: 400px 600px;
/* Global values */
scroll-snap-destination: inherit;
scroll-snap-destination: initial;
scroll-snap-destination: unset;
Syntax
Values
<position>
- Specifies the offset of the snap destination from the start edge of the scroll container’s visual viewport. The first value gives the x coordinate of the snap destination, the second value its y coordinate.
Formal definition
Initial value | 0px 0px |
---|---|
Applies to | scroll containers |
Inherited | no |
Percentages | relative to same axis of the padding-box of the scroll container |
Computed value | as specified, but with relative lengths converted into absolute lengths |
Animation type | a position |
Formal syntax
<position>where
<position> = [ [ left | center | right ] || [ top | center | bottom ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]? | [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]
where
<length-percentage> = <length> | <percentage>
Examples
Setting scroll snap destination
HTML
<div id="container">
<div>
<p>At coordinate (0, 0)</p>
<div class="scrollContainer destination0">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
<div>
<p>At coordinate (25, 0)</p>
<div class="scrollContainer destination25">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
<div>
<p>At coordinate (50, 0)</p>
<div class="scrollContainer destination50">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
</div>
CSS
#container {
display: flex;
}
#container > div:nth-child(-n+2) {
margin-right: 20px;
}
.scrollContainer {
width: 100px;
overflow: auto;
white-space: nowrap;
scroll-snap-points-x: repeat(100%);
scroll-snap-type: mandatory;
scroll-snap-destination: 20px 0;
font-size: 0;
}
.destination0 {
scroll-snap-destination: 0 0;
}
.destination25 {
scroll-snap-destination: 25px 0;
}
.destination50 {
scroll-snap-destination: 50px 0;
}
.scrollContainer > div {
width: 100px;
height: 100px;
display: inline-block;
line-height: 100px;
text-align: center;
font-size: 50px;
}
.scrollContainer > div:nth-child(even) {
background-color: #87EA87;
}
.scrollContainer > div:nth-child(odd) {
background-color: #87CCEA;
}
Result
Specifications
Not part of any standard.
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论