Fix object to top of browser window when scrolling

前端 未结 7 764
迷失自我
迷失自我 2020-12-04 16:55

I saw recently a new interesting feature in the new gmail and also in the HTML5 bing preview that fixes a navigation bar to the top of the browser window when scrolling. The

7条回答
  •  情深已故
    2020-12-04 17:36

    You can do it something like this :

    Example

    css

    body {
        height: 3000px;
        top: 0;
    
    }
    #image {
        width: 100%;
        background: #444;
        height: 50px;
    }
    #scroller{
        height:100px;
        background:#ccc;
    }
    .stuck{
        position:fixed;
        z-index:100;
        width:100%;
        top:0;
    }
    

    script

    $(window).scroll(function() {
                    if ($(window).scrollTop() > 50) {
                        $('#scroller').addClass('stuck');
                    } else {
                        $('#scroller').removeClass('stuck');
                    }
    
                });
    

    after scroll 50 px it will change the css of scroller.

    this may be a good start

提交回复
热议问题