How can I make a div stick to the top of the screen once it's been scrolled to?

后端 未结 21 1703
Happy的楠姐
Happy的楠姐 2020-11-22 07:12

I would like to create a div, that is situated beneath a block of content but that once the page has been scrolled enough to contact its top boundary, becomes fixed in place

21条回答
  •  無奈伤痛
    2020-11-22 08:01

    Not an exact solution but a great alternative to consider

    this CSS ONLY Top of screen scroll bar. Solved all the problem with ONLY CSS, NO JavaScript, NO JQuery, No Brain work (lol).

    Enjoy my fiddle :D all the codes are included in there :)

    CSS

    #menu {
    position: fixed;
    height: 60px;
    width: 100%;
    top: 0;
    left: 0;
    border-top: 5px solid #a1cb2f;
    background: #fff;
    -moz-box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
    -webkit-box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
    box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
    z-index: 999999;
    }
    
    .w {
        width: 900px;
        margin: 0 auto;
        margin-bottom: 40px;
    }

    Put the content long enough so you can see the effect here :) Oh, and the reference is in there as well, for the fact he deserve his credit

    CSS ONLY Top of screen scroll bar

提交回复
热议问题