Make element fixed on scroll

前端 未结 10 2410
臣服心动
臣服心动 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:36

    I wouldn't bother with jQuery or LESS. A javascript framework is overkill in my opinion.

    window.addEventListener('scroll', function (evt) {
    
      // This value is your scroll distance from the top
      var distance_from_top = document.body.scrollTop;
    
      // The user has scrolled to the tippy top of the page. Set appropriate style.
      if (distance_from_top === 0) {
    
      }
    
      // The user has scrolled down the page.
      if(distance_from_top > 0) {
    
      }
    
    });
    

提交回复
热议问题