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

后端 未结 21 1454
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 07:56

    As Josh Lee and Colin 't Hart have said, you could optionally just use position: sticky; top: 0; applying to the div that you want the scrolling at...

    Plus, the only thing you will have to do is copy this into the top of your page or format it to fit into an external CSS sheet:

    <style>
    #sticky_div's_name_here { position: sticky; top: 0; }
    </style>
    

    Just replace #sticky_div's_name_here with the name of your div, i.e. if your div was <div id="example"> you would put #example { position: sticky; top: 0; }.

    0 讨论(0)
  • 2020-11-22 07:57

    As of January 2017 and the release of Chrome 56, most browsers in common use support the position: sticky property in CSS.

    #thing_to_stick {
      position: sticky;
      top: 0px;
    }
    

    does the trick for me in Firefox and Chrome.

    In Safari you still need to use position: -webkit-sticky.

    Polyfills are available for Internet Explorer and Edge; https://github.com/wilddeer/stickyfill seems to be a good one.

    0 讨论(0)
  • 2020-11-22 07:57

    My solution is a little verbose, but it handles variable positioning from the left edge for centered layouts.

    // Ensurs that a element (usually a div) stays on the screen
    //   aElementToStick   = The jQuery selector for the element to keep visible
    global.makeSticky = function (aElementToStick) {
        var $elementToStick = $(aElementToStick);
        var top = $elementToStick.offset().top;
        var origPosition = $elementToStick.css('position');
    
        function positionFloater(a$Win) {
            // Set the original position to allow the browser to adjust the horizontal position
            $elementToStick.css('position', origPosition);
    
            // Test how far down the page is scrolled
            var scrollTop = a$Win.scrollTop();
            // If the page is scrolled passed the top of the element make it stick to the top of the screen
            if (top < scrollTop) {
                // Get the horizontal position
                var left = $elementToStick.offset().left;
                // Set the positioning as fixed to hold it's position
                $elementToStick.css('position', 'fixed');
                // Reuse the horizontal positioning
                $elementToStick.css('left', left);
                // Hold the element at the top of the screen
                $elementToStick.css('top', 0);
            }
        }
    
        // Perform initial positioning
        positionFloater($(window));
    
        // Reposition when the window resizes
        $(window).resize(function (e) {
            positionFloater($(this));
        });
    
        // Reposition when the window scrolls
        $(window).scroll(function (e) {
            positionFloater($(this));
        });
    };
    
    0 讨论(0)
  • 2020-11-22 07:59

    The accepted answer works but doesn't move back to previous position if you scroll above it. It is always stuck to the top after being placed there.

      $(window).scroll(function(e) {
        $el = $('.fixedElement');
        if ($(this).scrollTop() > 42 && $el.css('position') != 'fixed') {
          $('.fixedElement').css( 'position': 'fixed', 'top': '0px');
    
        } else if ($(this).scrollTop() < 42 && $el.css('position') != 'relative') {
          $('.fixedElement').css( 'relative': 'fixed', 'top': '42px');
    //this was just my previous position/formating
        }
      });
    

    jleedev's response whould work, but I wasn't able to get it to work. His example page also didn't work (for me).

    0 讨论(0)
  • 2020-11-22 08:01

    Not an exact solution but a great alternative to consider

    this CSS ONLY Top of screen scroll bar. Solved all the problem with ONLY CSS, NO JavaScript, NO JQuery, No Brain work (lol).

    Enjoy my fiddle :D all the codes are included in there :)

    CSS

    #menu {
    position: fixed;
    height: 60px;
    width: 100%;
    top: 0;
    left: 0;
    border-top: 5px solid #a1cb2f;
    background: #fff;
    -moz-box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
    -webkit-box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
    box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
    z-index: 999999;
    }
    
    .w {
        width: 900px;
        margin: 0 auto;
        margin-bottom: 40px;
    }<br type="_moz">
    

    Put the content long enough so you can see the effect here :) Oh, and the reference is in there as well, for the fact he deserve his credit

    CSS ONLY Top of screen scroll bar

    0 讨论(0)
  • 2020-11-22 08:05

    The info provided to answer this other question may be of help to you, Evan:

    Check if element is visible after scrolling

    You basically want to modify the style of the element to set it to fixed only after having verified that the document.body.scrollTop value is equal to or greater than the top of your element.

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