css实现滚动吸顶

282 阅读1分钟

吸顶元素样式:

.sticky {
  position: sticky;
  top: 0;
  padding: 10px 0;
  background-color: white;
  z-index: 20;
}

条件:

  • 父元素设置:overflow:hidden
  • 吸顶元素必须指定positiontop

扩展:实现吸底操作则只需将吸底元素的bottom设置为0即可。