简体   繁体   English

在滚动直到到达页脚时修复边栏(使用Bootstrap类)

[英]Fix a sidebar while scrolling until footer is reached (using Bootstrap classes)

I'm trying to make a sidebar that will scroll with the page, and when its bottom reaches the page footer, it will stop scrolling and stay in place. 我试图制作一个可随页面滚动的侧边栏,当其底部到达页面页脚时,它将停止滚动并保持在原位。 Like it is explained here , and like in this fiddle . 就像这里解释的一样,就像这个小提琴一样

The problem is, I'm using some Bootstrap 3, and I believe I can't achieve the same result because of some properties on its CSS. 问题是,我正在使用一些Bootstrap 3,并且由于其CSS上的某些属性,我相信无法获得相同的结果。 Instead of the sidebar staying at the bottom, it goes back up after it reaches the footer. 而不是侧边栏停留在底部,而是在到达页脚后又向上返回。

This is my layout structure. 这是我的布局结构。

<nav class="navbar"></nav>

<div class="container" id="wrapper">
  <div class="row">
     <div class="col-sm-8" id="blog-main"></div>
     <div class="col-sm-4" id="blog-sidebar"></div>
  </div>
</div>

<div id='blog-footer'></div>

Fiddle: https://jsfiddle.net/mpxrqwwf/7/ 小提琴: https : //jsfiddle.net/mpxrqwwf/7/

I tried putting blog-sidebar inside a wrapper div, with position: absolute on it and with position: relative on the main wrapper, but it broke the layout and the scrolling still did not work as I expected. 我尝试将blog-sidebar放在包装div中,其position: absoluteposition: relative ,位于主包装中,但它破坏了布局,滚动仍然无法按我预期的方式进行。 Thanks in advance for the help. 先谢谢您的帮助。

Bootstrap has a build in plugin to do that for you, just by adding a couple of data attributes. Bootstrap具有一个内置插件,只需添加几个数据属性即可为您完成此任务。

Affix affix.js 词缀 affix.js

The affix plugin toggles position: fixed; 词缀插件可切换位置:固定;固定; on and off, emulating the effect found with position: sticky;. 打开和关闭,模拟在以下位置发现的效果:粘性;

You just have to define an offset-top and an offset-bottom via the attributes on the element you want to be affixed. 您只需通过要固定的元素上的属性定义一个top-top和一个offset-bottom Based on your offset values the plugin will add and remove a couple of CSS classes when an offset is reached during scrolling. 根据您的偏移量值,插件将在滚动过程中达到偏移量时添加和删除几个CSS类。 These classes can be styled to your needs. 这些类可以根据您的需要设置样式。

Positioning via CSS 通过CSS定位

The affix plugin toggles between three classes, each representing a particular state: .affix, .affix-top, and .affix-bottom. 词缀插件在三个类之间切换,每个类代表一个特定的状态:.affix,.affix-top和.affix-bottom。 You must provide the styles, with the exception of position: fixed; 您必须提供样式,位置除外:固定; on .affix, for these classes yourself (independent of this plugin) to handle the actual positions 在.affix上,您自己(独立于此插件)通过这些类来处理实际位置

If you want more flexibility you can call the affix plugin via javaScript. 如果需要更大的灵活性,可以通过javaScript调用affix插件。

Here is your fiddle using the affix plugin via data attributes: 这是通过数据属性使用词缀插件的小提琴

CSS 的CSS

/** CSS relacionado à barra de navegação **/
#my-nav {
  position: fixed;
  width: 100%;
  background-color: black;
  height: 40px !important;
  min-height: 0px;
  z-index: 9999;
  margin-bottom: 0;
}

#wrapper {
  width: 81.6%;
}

#blog-header {
  color: black;
  text-align: right;
  margin-bottom: 20px;
}

#blog-main {
  /*width: 68%;*/
  height: 500px;
  padding: 0;
  margin-right: 5px;
  background-color: orange;
}

#blog-sidebar {
  background-color: purple;
  /*width: 30.9%;*/
  width: 222px;
  height: 300px;
  padding: 25px 0px 20px 0px;
}

.affix {
  position: fixed;
  top: 40px;
}
.affix-top {
  position: static;
}
.affix-bottom {
  position: absolute;
}

#blog-footer {
  background-color: green;
  height: 300px;
}

HTML 的HTML

<nav class="navbar" id="my-nav"></nav>

<br /><br />

<div class="container" id="wrapper">
  <div id="blog-header">
    <h1>header</h1>
  </div>
  <div class="row">
    <div class="col-sm-8">
      <div id="blog-main"></div>
    </div>
    <div class="col-sm-4">
      <div id="blog-sidebar" data-spy="affix" data-offset-top="80" data-offset-bottom="345"></div>
    </div>
  </div>
</div><!-- /.container -->
<div id='blog-footer'></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM