Make element fixed on scroll

前端 未结 10 2165
臣服心动
臣服心动 2020-12-23 02:09

I\'m attempting to make the navigation bar stick to the top, when the user scrolls down to the nav bar and then unstick when the user scrolls back up past the navbar. I unde

相关标签:
10条回答
  • 2020-12-23 02:17

    You can go to LESS CSS website http://lesscss.org/

    Their dockable menu is light and performs well. The only caveat is that the effect takes place after the scroll is complete. Just do a view source to see the js.

    0 讨论(0)
  • 2020-12-23 02:22

    You can do this with css too.

    just use position:fixed; for what you want to be fixed when you scroll down.

    you can have some examples here:

    http://davidwalsh.name/demo/css-fixed-position.php

    http://demo.tutorialzine.com/2010/06/microtut-how-css-position-works/demo.html

    0 讨论(0)
  • 2020-12-23 02:24
    window.addEventListener("scroll", function(evt) {
        var pos_top = document.body.scrollTop;   
        if(pos_top == 0){
           $('#divID').css('position','fixed');
        }
    
        else if(pos_top > 0){
           $('#divId').css('position','static');
        }
    });
    
    0 讨论(0)
  • 2020-12-23 02:27

    You want to use jQuery WayPoints. It is a very simple plugin and acheives exactly what you have described.

    Most straightforward implementation

        $('.thing').waypoint(function(direction) {
      alert('Top of thing hit top of viewport.');
    });
    

    You will need to set some custom CSS to set exactly where it does become stuck, this is normal though for most ways to do it.

    This page will show you all the examples and info that you need.

    For future reference a example of it stopping and starting is this website. It is a "in the wild" example.

    0 讨论(0)
  • 2020-12-23 02:28

    Here you go, no frameworks, short and simple:

    var el = document.getElementById('elId');
    var elTop = el.getBoundingClientRect().top - document.body.getBoundingClientRect().top;
    
    window.addEventListener('scroll', function(){
        if (document.documentElement.scrollTop > elTop){
            el.style.position = 'fixed';
            el.style.top = '0px';
        }
        else
        {
            el.style.position = 'static';
            el.style.top = 'auto';
        }
    });
    
    0 讨论(0)
  • 2020-12-23 02:28

    Plain Javascript Solution (DEMO) :

    <br/><br/><br/><br/><br/><br/><br/>
    <div>
      <div id="myyy_bar" style="background:red;"> Here is window </div>
    </div>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    
    
    <script type="text/javascript">
    var myyElement = document.getElementById("myyy_bar"); 
    var EnableConsoleLOGS = true;           //to check the results in Browser's Inspector(Console), whenever you are scrolling
    // ==============================================
    
    
    
    window.addEventListener('scroll', function (evt) {
        var Positionsss =  GetTopLeft ();  
        if (EnableConsoleLOGS) { console.log(Positionsss); }
        if (Positionsss.toppp  > 70)    { myyElement.style.position="relative"; myyElement.style.top = "0px";  myyElement.style.right = "auto"; }
        else                            { myyElement.style.position="fixed";    myyElement.style.top = "100px";         myyElement.style.right = "0px"; }
    });
    
    
    
    function GetOffset (object, offset) {
        if (!object) return;
        offset.x += object.offsetLeft;       offset.y += object.offsetTop;
        GetOffset (object.offsetParent, offset);
    }
    function GetScrolled (object, scrolled) {
        if (!object) return;
        scrolled.x += object.scrollLeft;    scrolled.y += object.scrollTop;
        if (object.tagName.toLowerCase () != "html") {          GetScrolled (object.parentNode, scrolled);        }
    }
    
    function GetTopLeft () {
        var offset = {x : 0, y : 0};        GetOffset (myyElement.parentNode, offset);
        var scrolled = {x : 0, y : 0};      GetScrolled (myyElement.parentNode.parentNode, scrolled);
        var posX = offset.x - scrolled.x;   var posY = offset.y - scrolled.y;
        return {lefttt: posX , toppp: posY };
    }
    // ==============================================
    </script>
    
    0 讨论(0)
提交回复
热议问题