css - 当您到达页面底部时,如何使页脚 div 将固定的 div 向上推

标签 css twitter-bootstrap-3 bootstrap-affix

我正在尝试模仿 this search results page 的布局。除了页脚之外,我所有的东西都可以使用 Bootstrap 附加来工作。当您滚动到我链接的示例的底部时,页脚会向上推 map div。这是我无法弄清楚的部分。当你向下滚动我的示例时,它就位于 map div 后面。如何让橙色页脚将 map div (#mapcontainer) 向上推?

CSS

    #topmenu.affix {
    top: 0;
    width: 100%;
    z-index: 9999 !important;
  }
      
  #mapcontainer {
      background: lightgray;
      margin-top: -20px;
      width: 100%;
      height: 100%;
      overflow: hidden;
  }
      
  #mapcontainer.affix {
    top: 72px;
    z-index: 9999 !important;
    
  }   
  

  #topmenu.affix + .container-fluid {
    padding-top: 70px;
  }
/*    
  #mapcontainer.affix + .container-fluid {
    padding-top: 100px;
     background: lightgray;
  }
*/     

footer {
  height: 100px;
  background-color: orange;
}
      
.container-fluid{
    padding-right: 0px;
}     
     
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">


</head>
<body>

<div class="container-fluid" style="background-color:#F44336;color:#fff;height:200px;">
  <h1>Bootstrap Affix Example</h1>
  <h3>Fixed (sticky) navbar on scroll</h3>
  <p>Scroll this page to see how the navbar behaves with data-spy="affix".</p>
  <p>The navbar is attached to the top of the page after you have scrolled a specified amount of pixels.</p>
</div>

<nav class="navbar navbar-inverse" id="topmenu" data-spy="affix" data-offset-top="197">
  <ul class="nav navbar-nav">
    <li class="active"><a href="#">Basic Topnav</a></li>
    <li><a href="#">Page 1</a></li>
    <li><a href="#">Page 2</a></li>
    <li><a href="#">Page 3</a></li>
  </ul>
</nav>

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3">
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
      <h1>Some text to enable scrolling</h1>
    </div>
    <div class="col-sm-9" >
        <div id="mapcontainer" data-spy="affix" data-offset-top="200">
        <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
            <h1>map area</h1>
        </div>
    </div>
  </div>
</div>
<footer class="bg-light text-center text-lg-start" id="footer">

  <div class="text-center p-3">
    hello
  </div>

</footer>
    

</body>
</html>

最佳答案

这是我的解决方案,只需要做出响应,并使用affixtopbottom进行调整。

#topmenu.affix {
  top: 0;
  width: 100%;
  z-index: 10000 !important;
}

#mapcontainer {
  background: lightgray;
  margin-top: -20px;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#topmenu.affix+.container-fluid {
  padding-top: 70px;
}

footer {
  height: 100px;
  background-color: orange;
}

.container-fluid {
  padding-right: 0px;
}


/*new code*/

.container-fluid .col-sm-9 {
  position: sticky;
  height: calc((100vh - 70px) - 0px);
  top: 70px;
}

#mapcontainer.affix {
  top: -20px;
  z-index: 9999 !important;
  position: relative;
  height: calc((100vh - 20px) - 10px);
}
<!DOCTYPE html>
<html>

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

</head>

<body>

  <div class="container-fluid" style="background-color:#F44336;color:#fff;height:200px;">
    <h1>Bootstrap Affix Example</h1>
    <h3>Fixed (sticky) navbar on scroll</h3>
    <p>Scroll this page to see how the navbar behaves with data-spy="affix".</p>
    <p>The navbar is attached to the top of the page after you have scrolled a specified amount of pixels.</p>
  </div>

  <nav class="navbar navbar-inverse" id="topmenu" data-offset-top="197" data-spy="affix">
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Basic Topnav</a></li>
      <li><a href="#">Page 1</a></li>
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>
    </ul>
  </nav>

  <div class="container-fluid">
    <div class="row">
      <div class="col-sm-3">
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
        <h1>Some text to enable scrolling</h1>
      </div>
      <div class="col-sm-9">
        <div id="mapcontainer" data-spy="affix" data-offset-top="200">
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
          <h1>map area</h1>
        </div>
      </div>
    </div>
  </div>
  <footer class="bg-light text-center text-lg-start" id="footer">

    <div class="text-center p-3">
      hello
    </div>

  </footer>


</body>

</html>

关于css - 当您到达页面底部时,如何使页脚 div 将固定的 div 向上推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75585138/

相关文章:

javascript - 响应式菜单在点击时闪烁

css - 修改导航栏时,如何正确呈现下拉菜单?

css - Bootstrap Affix 固定位置动态

javascript - bootstrap 附加插件内存泄漏

javascript - 具有固定标题和固定列的 Html 表

css - 通过后面的aspx代码添加css类

javascript - 当我的响应式菜单打开时,如何删除用户转到两侧的功能?

html - max-width 设置为 767px 时的媒体查询

javascript - Bootstrap 3 - IE9 Bug 上的不可见下拉列表。使固定?