How to build floating menu bar when scroll down

后端 未结 5 1269
感情败类
感情败类 2020-12-23 10:11

When I scrolled down site display black menu bar at the top look like float bar. but I think there\'s jquery involved with this. I have tried CSS but seems not working for m

5条回答
  •  旧时难觅i
    2020-12-23 11:02

    Try this

    CSS

    * {margin: 0; padding: 0; outline: 0;}
    
    
    body {
        font-family: Helvetica, Arial, Verdana, sans-serif;
        color: #999;
        font-size: 12px;
        background:#bfbfbf;
    
    
    }
    
    
    h1, h2 {
        font-family: 'Open Sans', sans-serif;
        font-weight: 300;
        margin:0 0 15px 0;
    }
    
    
    h1 {
        font-size: 36px;
        letter-spacing: -2px;
        line-height: 100%;
    }
    
    h1.title {
        font-size: 46px;
        font-weight: 700;
        color: #6a6a6a;
    
    
    }
    
    
    h2 {
        font-size: 24px;
    }
    
    p {
        margin: 0 0 15px 0;
    }
    
    .menuBtn {
    
        background: center center no-repeat transparent;
        background: #000;
        display: block;
        width: 40px;
        height: 40px;
        position: absolute;
        top: 0;
        left: 10px;
    
    }
    
    .clear {
        clear: both;
    }
    .wrap {
            /*background:url(../images/bg.png) top left repeat-x;*/
            width: 100%;
            max-width: 1140px;
            min-width: 960px;
            z-index: 10;
            position: relative;
            margin: 0 auto;
            padding: 0;
    
    }
    
    
    
    .section {
        width: 100%;
        max-width: 1140px;
        min-width: 960px;
        z-index: 10;
        position: relative;
        margin: 0 auto;
        padding: 0 0 20px 0;
    }
    
    
    .inner {
        width: 960px;
        margin: 0 auto;
        position: relative;
        min-height: 50px;
        padding:30px 0;
        font-size: 18px;
        font-family: 'Open Sans', sans-serif;
        font-weight: 300;
        padding:30px 0;
    }
    
    
    /* This is the selector i used for my menu, it needs to be set as position:absolute; */
    .subMenu {
        position: absolute;
        top: 462px;
        height: 50px;
        z-index: 1000;
        width: 100%;
        max-width: 1140px;
        min-width: 960px;
        background: #aabd46;
    
    }
    
    .subMenu .inner {
        padding:0;
        font-weight: 400;
    }
    
    
    
    
    
    .subNavBtn {
        display: block;
        height: 35px;
        width: 12%;
        float: left;
        margin: 0px 0px 0 0;
        text-decoration: none;
        font-size: 14px;
        padding: 15px 2% 0 2%;
        text-align: center;
        color: #fff;
    }
    
    .end {
        margin: 0;
    }
    
    
    /* SECTIONS */
    .sTop {
        min-height: 630px;
        background:#e5e5e5;
        color:#3d3d3d;
    
    }
    
    
    
    .s1 {
        min-height: 500px;
        background: #2e2e2e;
    }
    
    
    .s2 {
        min-height: 500px;
        background: #3f3f3f;
    }
    
    
    .s3 {
        min-height: 500px;
        background: #504f4f;
    }
    
    
    .s4 {
        min-height: 500px;
        background: #6e87a1;
        color: white;
    }
    
    .s5 {
        min-height: 500px;
        background: #293b4d;
        color: white;
    }
    

    JavaScript

    :

      (function(){
    
    
            $.fn.smint = function( options ) {
    
                // adding a class to users div
                $(this).addClass('smint')
    
                var settings = $.extend({
                    'scrollSpeed '  : 500
                    }, options);
    
    
                return $('.smint a').each( function() {
    
    
                    if ( settings.scrollSpeed ) {
    
                        var scrollSpeed = settings.scrollSpeed
    
                    }
    
    
                    ///////////////////////////////////
    
                    // get initial top offset for the menu 
                    var stickyTop = $('.smint').offset().top;   
    
                    // check position and make sticky if needed
                    var stickyMenu = function(){
    
                        // current distance top
                        var scrollTop = $(window).scrollTop(); 
    
                        // if we scroll more than the navigation, change its position to fixed and add class 'fxd', otherwise change it back to absolute and remove the class
                        if (scrollTop > stickyTop) { 
                            $('.smint').css({ 'position': 'fixed', 'top':0 }).addClass('fxd');
    
                            } else {
                                $('.smint').css({ 'position': 'absolute', 'top':stickyTop }).removeClass('fxd'); 
                            }   
                    };
    
                    // run function
                    stickyMenu();
    
                    // run function every time you scroll
                    $(window).scroll(function() {
                         stickyMenu();
                    });
    
                    ///////////////////////////////////////
    
    
                    $(this).on('click', function(e){
    
    
                        // gets the height of the users div. This is used for off-setting the scroll so th emenu doesnt overlap any content in the div they jst scrolled to
                        var selectorHeight = $('.smint').height();   
    
                        // stops empty hrefs making the page jump when clicked
                        e.preventDefault();
    
                        // get id pf the button you just clicked
                        var id = $(this).attr('id');
    
                        // gets the distance from top of the div class that matches your button id minus the height of the nav menu. This means the nav wont initially overlap the content.
                        var goTo =  $('div.'+ id).offset().top -selectorHeight;
    
                        // Scroll the page to the desired position!
                        $("html, body").animate({ scrollTop: goTo }, scrollSpeed);
    
                    }); 
    
    
    
                });
    
            }
    
        })();
    

    HTML

    
    
    
    SMINT Demo
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        

    Welcome to my site

    Click the links below !


    About me

    Portfolio

    Working Standards

    Hello

    Lets Go

提交回复
热议问题