javascript - 修复了上面的 Div 滚动过去时的标题

标签 javascript jquery header fixed

我有这样的设置,以便当页面滚动 400px 时,#headermenu 变得固定。然而,上面的 div 的高度会根据屏幕尺寸而变化。

当上面的 div 底部(#mixedheightheader)到达窗口顶部时,我需要 JS 使 #headermenu 变得固定。

JSFIDDLE

预先感谢您的帮助

<div id="mixedheightheader"></div>

$(function() {    
  $('#headermenu');
});

$(window).scroll(function() {    
  if ($(document).scrollTop() < 400)     {        
    if ($('#headermenu'))         {            
      $('#headermenu');            
      $('#headermenu').stop().css({
        top: '0',
        position: 'relative'
      });        
    }    
  }    
  else     {        
    if ($('#headermenu'))         {            
      $('#headermenu');            
      $('#headermenu').stop().css({
        top: '0',
        position: 'fixed'
      });        
    }      
  }
});
body {
  height: 3000px
}

#headermenu {
  width: 100%;
  background: black;
  min-height: 100px;
}

#mixedheightheader {
  top: 0;
  bottom: 0;
  width: 100%;
  height: 100vh;
  min-height: 200px;
  overflow: hidden;
  background: grey;
  clear: both;
}

#below {
  width: 100%;
  background: darkgrey;
  height: 100px;
  position: relative;
  z-index: -1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mixedheightheader"></div>
<div id="headermenu"></div>
<div id="below"></div>

最佳答案

我已经更新了你的 fiddle :

https://jsfiddle.net/yLon2oj3/11/

$(function() {    
  var isFixed = false; //This is to not fix if already fixed and reverse

    $(window).scroll(function() {
    var mixedHeightHeader = $('#mixedheightheader');
    //This uses the offset top position and the height to calculate the bottom of your variable header
    var mixedHeightHeaderBottom = mixedHeightHeader.offset().top + mixedHeightHeader.height();
    var headerMenu = $('#headermenu');


    if ($(document).scrollTop() < mixedHeightHeaderBottom && isFixed)       {        
        if (headerMenu) {                  
        headerMenu.css({
            top: '0',
            position: 'relative'
        });  
        isFixed = false;
        //this is to remove the placeholder space of the fixed top nav, when its not fixed to top
        mixedHeightHeader.css('margin-bottom', '0');
      }    
    }    
    else  if ($(document).scrollTop() > mixedHeightHeaderBottom && !isFixed)   {        
      if (headerMenu) {                      
        headerMenu.css({
          top: '0',
          position: 'fixed'
        });
        //This holds the position that was occupied by the fixed top nav when it was a relative element, because its now taken out of the flow.
        mixedHeightHeader.css('margin-bottom', headerMenu.height() + 'px');
      }
      isFixed = true;
    }
    });

});

关于javascript - 修复了上面的 Div 滚动过去时的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877149/

相关文章:

javascript - Spring MVC 应用程序中 RequireJS 库加载缓慢

jquery - css3 宽度过渡不适用于图像

c++ - 如何在头文件库中定义(非方法)函数

wordpress - 如何使get_header()调用位于主题子文件夹中的自定义头文件?

javascript - 如何避免 'frame got detached' 错误异步验证或使用 Puppeteer 重定向?

javascript - AngularJS,使用 Rest 服务,该服务以 Array<Object> 进行响应。如何在表格中显示这一点?

javascript - 如何创建临时文本框

javascript - 检测弹出窗口何时关闭

angular - 在获取请求 Angular 2 上设置 header

javascript - svg 动画标签改变