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

后端 未结 21 1857
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:11

    Here's an example that uses jquery-visible plugin: http://jsfiddle.net/711p4em4/.

    HTML:

    Header
    Content
    Footer

    CSS:

    * {
        margin: 0;
        padding: 0;
    }
    
    body {
        background-color: #e2e2e2;
    }
    
    .wrapper > header,
    .wrapper > footer {
        font: 20px/2 Sans-Serif;
        text-align: center;
        background-color: #0040FF;
        color: #fff;
    }
    
    .wrapper > main {
        position: relative;
        height: 500px;
        background-color: #5e5e5e;
        font: 20px/500px Sans-Serif;
        color: #fff;
        text-align: center;
        padding-top: 40px;
    }
    
    .wrapper > main > nav {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        font: 20px/2 Sans-Serif;
        color: #fff;
        text-align: center;
        background-color: #FFBF00;
    }
    
    .wrapper > main > nav.fixed {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
    }
    

    JS (include jquery-visible plugin):

    (function($){
    
        /**
         * Copyright 2012, Digital Fusion
         * Licensed under the MIT license.
         * http://teamdf.com/jquery-plugins/license/
         *
         * @author Sam Sehnert
         * @desc A small plugin that checks whether elements are within
         *       the user visible viewport of a web browser.
         *       only accounts for vertical position, not horizontal.
         */
        var $w = $(window);
        $.fn.visible = function(partial,hidden,direction){
    
            if (this.length < 1)
                return;
    
            var $t        = this.length > 1 ? this.eq(0) : this,
                t         = $t.get(0),
                vpWidth   = $w.width(),
                vpHeight  = $w.height(),
                direction = (direction) ? direction : 'both',
                clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true;
    
            if (typeof t.getBoundingClientRect === 'function'){
    
                // Use this native browser method, if available.
                var rec = t.getBoundingClientRect(),
                    tViz = rec.top    >= 0 && rec.top    <  vpHeight,
                    bViz = rec.bottom >  0 && rec.bottom <= vpHeight,
                    lViz = rec.left   >= 0 && rec.left   <  vpWidth,
                    rViz = rec.right  >  0 && rec.right  <= vpWidth,
                    vVisible   = partial ? tViz || bViz : tViz && bViz,
                    hVisible   = partial ? lViz || rViz : lViz && rViz;
    
                if(direction === 'both')
                    return clientSize && vVisible && hVisible;
                else if(direction === 'vertical')
                    return clientSize && vVisible;
                else if(direction === 'horizontal')
                    return clientSize && hVisible;
            } else {
    
                var viewTop         = $w.scrollTop(),
                    viewBottom      = viewTop + vpHeight,
                    viewLeft        = $w.scrollLeft(),
                    viewRight       = viewLeft + vpWidth,
                    offset          = $t.offset(),
                    _top            = offset.top,
                    _bottom         = _top + $t.height(),
                    _left           = offset.left,
                    _right          = _left + $t.width(),
                    compareTop      = partial === true ? _bottom : _top,
                    compareBottom   = partial === true ? _top : _bottom,
                    compareLeft     = partial === true ? _right : _left,
                    compareRight    = partial === true ? _left : _right;
    
                if(direction === 'both')
                    return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop)) && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
                else if(direction === 'vertical')
                    return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop));
                else if(direction === 'horizontal')
                    return !!clientSize && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
            }
        };
    
    })(jQuery);
    
    $(function() {
        $(window).scroll(function() {
            $(".wrapper > header").visible(true) ?
                $(".wrapper > main > nav").removeClass("fixed") :
                $(".wrapper > main > nav").addClass("fixed");
        });
    });
    

提交回复
热议问题