我一直在寻找一个解决我的粘性边栏问题的方法.我有一个具体的 idea ,我希望它如何行动;实际上,我希望它在你向下滚动时粘到底部,然后一旦你向上滚动,我希望它以流体运动(无 skip )粘到顶部.我找不到一个我试图实现的例子,所以我创建了一个图像,希望能更清楚地说明这一点:

Sticky sidebar: stick to bottom when scrolling down, top when scrolling up

  1. 侧边栏位于标题下方.
  2. 当您向下滚动时,侧边栏与页面内容保持水平,以便您可以滚动侧边栏和内容.
  3. Reach the bottom of the sidebar, the sidebar sticks to the bottom of the viewport (most plugins only allow for sticking to top, some that allow for sticking to bottom don't allow for both).
  4. Reach the bottom, sidebar sits above the footer.
  5. As you scroll back up, the sidebar stays level with the content so you can scroll through the content and sidebar again.
  6. Reach the top of the sidebar, the sidebar sticks to the top of the viewport.
  7. Reach the top and the sidebar sits back below the header.

I hope this is enough information. I have created a jsfiddle to test any plugins/scripts, which I have reset for this question: http://jsfiddle.net/jslucas/yr9gV/2/ .

推荐答案

+1美元,这是一幅非常好的插图.

我知道这是一个老问题,但我偶然发现了你在forum.jquery.com中发布的同一个问题,还有一个答案(by@tucker973),建议一个不错的图书馆来制作这个,并想在这里分享.

它叫sticky-kit@leafo

这里有我准备的一个非常基本的例子的代码和一个可以看到结果的工作演示.

/*!
 * Sticky-kit
 * A jQuery plugin for making smart sticky elements
 *
 * Source: http://leafo.net/sticky-kit/
 */

$(function() {
  $(".sidebar").stick_in_parent({
    offset_top: 10
  });
});
* {
  font-size: 10px;
  color: #333;
  box-sizing: border-box;
}
.wrapper,
.header,
.main,
.footer {
  padding: 10px;
  position: relative;
}
.wrapper {
  border: 1px solid #333;
  background-color: #f5f5f5;
  padding: 10px;
}
.header {
  background-color: #6289AE;
  margin-bottom: 10px;
  height: 100px;
}
.sidebar {
  position: absolute;
  padding: 10px;
  background-color: #ccc;
  height: 300px;
  width: 100px;
  float: left;
}
.main {
  background-color: #ccc;
  height: 600px;
  margin-left: 110px;
}
.footer {
  background-color: #6289AE;
  margin-top: 10px;
  height: 250px;
}
.top {
  position: absolute;
  top: 10px;
}
.bottom {
  position: absolute;
  bottom: 10px;
}
.clear {
  clear: both;
  float: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://leafo.net/sticky-kit/src/jquery.sticky-kit.js"></script>
<div class="wrapper">
  <div class="header"> <a class="top">header top</a>
    <a class="bottom">header bottom</a>

  </div>
  <div class="content">
    <div class="sidebar"> <a class="top">sidebar top</a>
      <a class="bottom">sidebar bottom</a>

    </div>
    <div class="main"> <a class="top">main top</a>
      <a class="bottom">main bottom</a>

    </div>
    <div class="clear"></div>
  </div>
  <div class="footer"> <a class="top">footer top</a>
    <a class="bottom">footer bottom</a>

  </div>
</div>

Of course, all credits go to the creator of the plugin, I only made this example to show it here. I need to accomplish the same result that you was after and found this plugin very useful.

Jquery相关问答推荐

通过 Ajax POST 将按钮值从 HTML 发送到 Flask 时出现空数据

Flask,如何为ajax调用返回成功状态码

在 div 中找到第一次出现的类

在 jQuery UI Datepicker 中禁用future 日期

使用 jQuery 和 HTML 导出为 CSV

如何使用 jquery 正确格式化货币?

在没有 jQuery 的情况下在 node.js 上合并或合并 JSON

JQUERY UI Accordion 开始折叠

jQuery中追加的相反

使用 jQuery DataTables 时禁用最后一列的排序

将返回的 JSON 对象属性转换为(较低的第一个)camelCase

jQuery ajax 在 asp.net mvc 中上传文件

jQuery XML 错误请求的资源上不存在‘Access-Control-Allow-Origin’标头.

.append 之后的 jQuery 函数

如何在一个元素上拥有多个数据绑定属性?

我可以通过 JavaScript 禁用 CSS :hover 效果吗?

通过 javascript/jquery 删除/截断前导零

D3 和​​ jQuery 有什么区别?

如何使用 ID 变量在 jQuery 中 Select 元素?

jQuery append() 与 appendChild()