这篇文章给大家分享的是有关CSS粘性定位position sticky用法详解的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

sticky
css属性position为sticky的元素,根据正常的文档流(flow of the document)进行定位,然后相对它的最近滚动祖先(nearest scrolling ancestor)和 containing block (最近块级祖先 nearest block-level ancestor),包括table-related元素,基于top, right, bottom, 和 left的值进行偏移。该偏移量不会影响任何其他元素的位置。
sticky元素总是创建一个新的层叠上下文(stacking context) ,一个sticky元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的overflow 是 hidden, scroll, auto, 或 overlay时),即便这个祖先不是最近的真实可滚动祖先。(Github issue on W3C CSSWG)
下面看一个关于粘性定位元素滑动时的效果图。
可以看到list在向上滑动时,当 list item 17 stickyTop 滑到在滚动块的顶部时,便粘在顶部不在跟着滑动,其余元素继续滑动,该元素的偏移量 top 为0。当list向下滑动式 list item 24 stickyBottom 滑到底部时便粘在底部,其余元素继续滑动,该元素的偏移量 bottom 为0。
list item 17 stickyTop 和 list item 24 stickyBottom 的css属性如下:
// list item 17 stickyTop
{
position: sticky;
top: 0;
background: aqua;
}
// list item 24 stickyBottom
{
position: sticky;
bottom: 0;
background: aqua;
}修改偏移量后再次尝试
list item 17 stickyTop 和 list item 24 stickyBottom 的css属性如下:
// list item 17 stickyTop
{
position: sticky;
top: 30px;
background: aqua;
}
// list item 24 stickyBottom
{
position: sticky;
bottom: 40px;
background: aqua;
}修改之后,17 在滑动时粘在距离顶部30px的位置,24 粘在距离底部40px的位置。
import React from 'react';
import classnames from 'classnames';
import styles from './dashboard.less';
const arr = new Array(40).fill({});
const list = arr.map((val, idx) => {
if (idx === 17) {
return {
...val,
key: idx,
title: 'stickyTop',
sticky: true,
className: styles.stickyTop,
};
}
if (idx === 24) {
return {
...val,
key: idx,
title: 'stickyBottom',
sticky: true,
className: styles.stickyBottom,
};
}
return { ...val, key: idx };
});
export default () => {
return (
{list.map((v, index) => (
{`list item ${index} ${v.title || ''}`}
分享题目:CSS粘性定位positionsticky用法详解-创新互联
网站URL:http://cdysf.com/article/dpgehp.html