CSS粘性定位position sticky用法详解
创始人
2024-11-18 22:42:57
0次

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)

实际效果展示

下面看一个关于粘性定位元素滑动时的效果图。
CSS粘性定位position sticky用法详解

可以看到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;
}

修改偏移量后再次尝试
CSS粘性定位position sticky用法详解
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的位置。

应用场景

  • 长列表滑动时,最近子标题固定(粘在)顶部
  • 表格的表头、首行、首列、末列滑动时分别固定顶部、顶部、左侧、右侧
  • 页面滑动时,重要信息块粘在顶部
  • 页面滑动时,特定元素始终保持在页面之内。

Note

  • 注意分清粘性定位的作用域,层叠上下文,避免因多个滚动块或者滚动块层级有误造成未达到目标效果

案例代码

jsx

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 || ''}`}
                     ))}
                 );
};

less

.container {
  width: 400px;
  height: 500px;
  margin: auto;
  overflow: auto;
  background: aquamarine;
  .wrap {
    display: flex;
    height: 1000px;
    .list {
      width: 240px;
      height: 360px;
      margin: auto;
      overflow: auto;
      background: #fff;
      .item {
        color: #333;
        font-size: 16px;
      }
      .sticky {
        background: aqua;
      }
      .stickyTop {
        position: sticky;
        top: 0;
      }
      .stickyBottom {
        position: sticky;
        bottom: 0;
      }
    }
  }
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...