position:sticky

1、position:sticky简介

设置了position:sticky的元素并不会脱离文档流。

  • 当元素在区域内,元素不受定位的影响(topleft等设置无效)
  • 当发生滚动,元素就要移出区域时,定位又会变成fixed,根据设置的lefttop的值进行定位,像是fixed效果
  <div class="sticky">
        position:sticky
    </div>

   .sticky{
      width:300px;
      height: 40px;
      background-color: lightseagreen;
      position:sticky;
      top:0;
   }

在这里插入图片描述

2、必须注意的点

设置了position:sticky的元素的效果完全受制于父级元素。

此外,还遵循以下条件

  1. 父级元素不能有任何overflow:visible以外的overflow设置。即使是scroll或者auto也不行。
  2. 父元素的高度不能低于sticky元素的高度
  3. 同一父容器中的sticky元素,如果定位置相等,则会发生重叠,
  4. sticky元素如果不属于相同父元素,但是他們的父元素正好紧密相连,则会挤开原来的元素,形成依次占位的效果。
  5. 必须制定top,left,bottom,right四个中的至少一个,否则其行为与相对定位相同,并且topbottom同时设置时,top的优先级高;leftright同时设置时,left的优先级高。

3、简单示例

3.1 同一父容器中的sticky元素
    <div class="sticky">
        <div>孵化基地发挥地方发挥地方</div>
        <div>孵化基地发挥地方发挥地方</div>
        <div>孵化基地发挥地方发挥地方</div>
    </div>
        .sticky {
            width:300px;
            height: 400px;             
        }
        .sticky div{
            position: sticky;
            top:0px;
            height: 60px;
            background-color: lightpink;
            margin-bottom: 20px;
        }

在这里插入图片描述
可以看到,这些stick元素位于同一个父容器内时,当滚动的一定位置时,后面的会覆盖前面的

3.2 sticky元素不属于同一父元素,但父元素正好紧密相连

(为了录制 细节 以及手动操作的 的原因,看着不是很流畅。。。。。实际上很顺滑的。。。)
在这里插入图片描述

        article h4 {   
            position: sticky;   
            background-color: lightseagreen;
            color: #fff;
            padding: 10px;
            top: 0;       
        }
        article div{
            background-color: lightskyblue;
        }
    <article>
        <section>
            <h4>
                湖人捧奖杯致敬科比鲁UT4923乘客请检测进博会实行核酸检测丰田c_hr青岛已排查密接132人
            </h4>
            <div>
                http.createServer(function (request, response) {
                console.log(request.url,request.pathname,'---------')
                //var pathname=url.parse(request.url).pathname;
                var pathname=(request.url).slice(1)||"drag.html";
                console.log(" 请求 "+pathname+"收到了");
            </div>
        </section>
        <section>
            <h4>
                湖人捧奖杯致敬科比鲁UT4923乘客请检测进博会实行核酸检测丰田c_hr青岛已排查密接132人
            </h4>
            <div>
                http.createServer(function (request, response) {
                console.log(request.url,request.pathname,'---------')
                //var pathname=url.parse(request.url).pathname;
                var pathname=(request.url).slice(1)||"drag.html";
                console.log(" 请求 "+pathname+"收到了");
            </div>
        </section>
        <section>
            <h4>
                湖人捧奖杯致敬科比鲁UT4923乘客请检测进博会实行核酸检测丰田c_hr青岛已排查密接132人
            </h4>
            <div>
                http.createServer(function (request, response) {
                console.log(request.url,request.pathname,'---------')
                //var pathname=url.parse(request.url).pathname;
                var pathname=(request.url).slice(1)||"drag.html";
                console.log(" 请求 "+pathname+"收到了");
            </div>
        </section>  
    </article>
  • 33
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值