How to keep the header static, always on top while scrolling?

前端 未结 8 825
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 08:15

How would I go about keeping my header from scrolling with the rest of the page? I thought about utilizing frame-sets and iframes, jus

8条回答
  •  星月不相逢
    2020-12-02 09:07

    here is one with css + jquery (javascript) solution.

    here is demo link Demo

    //html
    
    
    
    //css 
    
    #uberbar    { 
        border-bottom:1px solid #eb7429; 
        background:#fc9453; 
        padding:10px 20px; 
        position:fixed; 
        top:0; 
        left:0; 
        z-index:2000; 
        width:100%;
    }
    
    //jquery
    
    $(document).ready(function() {
        (function() {
            //settings
            var fadeSpeed = 200, fadeTo = 0.5, topDistance = 30;
            var topbarME = function() { $('#uberbar').fadeTo(fadeSpeed,1); }, topbarML = function() { $('#uberbar').fadeTo(fadeSpeed,fadeTo); };
            var inside = false;
            //do
            $(window).scroll(function() {
                position = $(window).scrollTop();
                if(position > topDistance && !inside) {
                    //add events
                    topbarML();
                    $('#uberbar').bind('mouseenter',topbarME);
                    $('#uberbar').bind('mouseleave',topbarML);
                    inside = true;
                }
                else if (position < topDistance){
                    topbarME();
                    $('#uberbar').unbind('mouseenter',topbarME);
                    $('#uberbar').unbind('mouseleave',topbarML);
                    inside = false;
                }
            });
        })();
    });
    

提交回复
热议问题