Getting a sticky header to “push up”, like in Instagram's iPhone app using CSS and jQuery

前端 未结 8 1336
[愿得一人]
[愿得一人] 2020-11-30 16:12

The Instagram app has a nice sticky header that pushes the current one up in place of the new one. I found a great tutorial on how to do this natively for Android, but I\'m

相关标签:
8条回答
  • 2020-11-30 17:08

    Make sure to wrap it in div

    Notice the 6th heading as it is not wrapped in div

    h1, h2{
    position: sticky;
    top: 0;
    }
    
    div{
    height: 500px;
    }
    <div>
    <h1>lorem ipsum doloro sit <br> amet 1</h1>
    </div>
    
    <div>
    <h1>lorem ipsum doloro sit 2</h1>
    </div>
    
    <div>
    <h2>lorem ipsum doloro sit 3</h2>
    </div>
    
    <div>
    <h1>lorem ipsum doloro sit <br> amet 4</h1>
    </div>
    
    <div>
    <h2>lorem ipsum doloro sit <br> amet 5</h2>
    </div>
    
    <h2>lorem ipsum doloro sit <br> amet 6</h2>
    
    <div></div>
    
    <div>
    <h1>lorem ipsum doloro sit  7</h1>
    </div>
    
    <div>
    <h1>lorem ipsum doloro sit <br> amet 8</h1>
    </div>
    
    <div>
    <h1>lorem ipsum doloro sit <br> amet 9</h1>
    </div>

    0 讨论(0)
  • 2020-11-30 17:12

    A basic version of the solution:

    CSS

    .sectionWrapper{
        background-color: #1E1E1E;
        padding: 10px 20px;
        color: #FF7B05;
        width: 100%
        height: 45px;
        min-height: 45px;
    
        margin-top: 30px;   
        position: relative;
        z-index: 10;
    }
    .sectionHeader{
    }
    .sectionEmbed {
    }
    .sectionFixed {
        background-color: #1E1E1E;
        padding: 10px 20px;
        color: #FF7B05;
        width: 100%;
        height: 45px;
    
        position: fixed;
        top: 0;
        left: 0;
        margin: 0;
    }
    

    HTML

    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 sectionWrapper sectionEmbed">
        <div class="sectionHeader">Headers here</div>
    </div>
    

    JQuery

    //check doc is ready
    $(function() {
        $(window).on("scroll.sectionWrapper", function() {
            stickSections();
        });
    });
    
    //function stickSection
    function stickSections()
    {
        //detach the listener
        $(window).off("scroll.sectionWrapper");
    
        $scrollPos = $(window).scrollTop();
        $currentSticky = null;
    
        $('.sectionWrapper').each(function(){
    
            $(this).children().removeClass('sectionFixed');     
    
            if($scrollPos >= ($(this).offset().top - ($(this).height() - $(this).children().height() - 6)) ){
                $currentSticky = $(this);
            }
            else $(this).children().addClass('sectionEmbed');
        });
    
        //apply the fixed class to our scrollPos match
        if($currentSticky) $currentSticky.children().addClass('sectionFixed');
    
        //reattach the listener
        $(window).on("scroll.sectionWrapper", function() {
            stickSections();        
        });
    }
    

    EXAMPLE

    https://codepen.io/ZombiesByte/pen/EyYwVO

    Have fun :)

    0 讨论(0)
提交回复
热议问题