position:sticky粘性/悬浮定位

目录

1、通俗易懂

2、sticky介绍

3、举例(Vue2为例)


1、通俗易懂

  • position:sticky可以简单理解为是static和fixed的结合;
  • 可理解为在父元素滑动过程中,子元素距离其父元素的距离达到 sticky 粘性定位的要求时(如top:40px);position:sticky这时的效果相当于fixed定位,固定到适当位置。

2、sticky介绍

  • sticky只能在最接近的父元素为overflow中使用;
  • 至少规定top, left ,right, bottom中的一种, 否则stikcy定位不生效,最终效果类似于relative;
  • 假设定义了top(bottom)属性. 那么父元素height不能低于top(bottom)的值;
  • 使用前请检查兼容性

3、举例(Vue2为例)

如上图二:滑动后,红色div脱离原本的蓝色框所在文档流,最终悬浮在父容器top:40px处

 

<template>
  <div class="box">
    <div class="box-scroll">
      <div v-for="(item, index) in 10" :key="index">{{ index + 1 }}</div>
      <div class="box-scroll-sticky">我要悬浮在40px处</div>
      <div v-for="(item, index) in 100" :key="index">{{ index + 1 }}</div>
    </div>
  </div>
</template>
<script>
export default {};
</script>
<style lang="scss" scoped>
.box {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  &-scroll {
    height: 400px;
    width: 400px;
    background: #eee;
    overflow: auto; //父元素必须是scroll
    display: flex;
    flex-direction: column;
    position: relative;
    > div {
      width: 100%;
      height: 40px;
      border-bottom: 2px solid #fff;
    }
    &-sticky {
      position: sticky;
      top: 40px; //至少设置一个
      background: red;
      color: #fff;
    }
  }
}
</style>

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
position: sticky是一种CSS属性,用于实现粘性布局,也可以理解为实现吸顶效果。它的作用是让某个元素在滚动过程中保持固定位置,同时又不会影响到其他元素的布局。当修饰的目标节点在屏幕中时,它表现为相对定位(relative),当要超出屏幕时,它表现为固定定位(fixed)。这种特性结合了相对定位和固定定位的特点,使得元素在滚动过程中可以保持在指定位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JS解决position:sticky的兼容性问题的方法](https://download.csdn.net/download/weixin_38741950/13206331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [position: sticky属性的基本使用](https://blog.csdn.net/weixin_43636361/article/details/126707014)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [position:sticky粘性/悬浮定位](https://blog.csdn.net/Chuinj/article/details/130714048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值