位置固定在溢出容器中 - scrollToFixed

时间:2015-06-29 17:51:39

标签: jquery html css

我正在尝试使用名为scrollToFixed的插件来修复滚动标题,就像iOS联系人列表一样。我无法看到如何使标题(.learning-objective-header)固定并相对于容器。

    <div class="panel-container">
  <div class="title">Assessment - Year 1 - Summer Half Term</div>
  <div class="content">
    <div class="nav-bar">
      <select><option>English 80%</option></select>
      <button class="submit-btn">Send Assessment</button>
    </div>
    <div class="column-container">
      <div class="review-screen-wrapper">
        <div class="review-screen perfect-scroll">
          <div class="learning-objective-header">
            <table>
              <thead>
                <tr>
                  <th class="col-1 learning-objective-label">Writing</th>
                  <th class="col-2 marking-scheme-positive">I can</th>
                  <th class="col-3 marking-scheme-neutral">Nearly there</th>
                  <th class="col-4 marking-scheme-negative">I need help</th>
                </tr>
              </thead>
            </table>
          </div>
          <div class="learning-objective-body">
            <table>
              <tbody>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class="learning-objective-header">
            <table>
              <thead>
                <tr>
                  <th class="col-1 learning-objective-label">Writing</th>
                  <th class="col-2 marking-scheme-positive">I can</th>
                  <th class="col-3 marking-scheme-neutral">Nearly there</th>
                  <th class="col-4 marking-scheme-negative">I need help</th>
                </tr>
              </thead>
            </table>
          </div>
          <div class="learning-objective-body">
            <table>
              <tbody>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
                <tr>
                  <td class="col-1 sub-learning-objective-label">Can I use a captial letter?</td>
                  <td class="col-2 marking-scheme-positive"><span class="marking-scheme-input"></span></td>
                  <td class="col-3 marking-scheme-neutral"><span class="marking-scheme-input"></span></td>
                  <td class="col-4 marking-scheme-negative"><span class="marking-scheme-input"></span></td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

@import "http://fonts.googleapis.com/css?family=Open+Sans:400,600,700";
.panel-container ,
.panel-container * {
  box-sizing: border-box;
}
body {
  background: #014783;
  color: #fff;
  font-family: "Open Sans";
}
.panel-container {
  background: #006bb3;
  border: 2px solid #b5e4fe;
  border-radius: 8px;
  margin: 20px;
  position: relative;
  height: calc(100vh - 40px);
}
.title {
  font-size: 22px;
  background: #0089da;
  padding: 15px 22px;
  border-radius: 6px 6px 0 0;
}
.nav-bar {
  text-align: left;
}
.submit-btn {
  float: right;
}
.content {
  padding: 22px;
  position: relative;
}
.column-container:after {
  content: " ";
  display: table;
  clear: both;
}
.review-screen-wrapper {
  position: relative;
  margin-top: 22px;
  border-radius: 5px;
  color: #444;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
  table-layout: fixed;
  width: 100%;
}
.review-screen {
}
.review-screen.perfect-scroll {
  height: calc(100vh - 184px);
  position: relative;
  overflow-y: scroll;
  border-radius: 5px;
}
.review-screen table th,
.review-screen table td {
  padding: 15px;
  vertical-align: middle;
  text-align: left;
}
.review-screen table th {
  font-size: 18px;
  font-weight: 600;
  padding: 13px 15px;
}
.learning-objective-header ~ .learning-objective-header table th.col-1 {
  border-top: 1px solid #CBCBCB;
}
.review-screen table th.col-1 {
  background: #F5F5F5;
  border-bottom: 1px solid #CBCBCB;
}
.learning-objective-header ~ .learning-objective-header table .col-1 {
  border-top: 1px solid #E2E2E2;
}
.review-screen table .col-1 {
  border-bottom: 1px solid #E2E2E2;
}
.learning-objective-header ~ .learning-objective-header table .col-2 {
  border-top: 1px solid #A7CB72;
}
.review-screen table th.col-2 {
  border-bottom: 1px solid #A7CB72;
}
.review-screen table .col-2 {
  border-bottom: 1px solid #BAE083;
}
.learning-objective-header ~ .learning-objective-header table .col-3 {
  border-top: 1px solid #E5C46D;
}
.review-screen table th.col-3 {
  border-bottom: 1px solid #E5C46D;
}
.review-screen table .col-3 {
  border-bottom: 1px solid #EDD9A4;
}
.learning-objective-header ~ .learning-objective-header table .col-4 {
  border-top: 1px solid #F9959D;
}
.review-screen table th.col-4 {
  border-bottom: 1px solid #F9959D;
}
.review-screen table .col-4 {
  border-bottom: 1px solid #F7AFB5;
}
.review-screen table .col-2,
.review-screen table .col-3,
.review-screen table .col-4 {
  width: 160px;
  text-align: center;
}
.review-screen table .col-1 {
  background: #FFF;
}
.review-screen table .col-2 {
  background: #ddecd5;
  color: #81a34f;
}
.review-screen table .col-3 {
  background: #fdeec7;
  color: #be9424;
}
.review-screen table .col-4 {
  background: #fecacc;
  color: #d16972;
}
.review-screen .learning-objective-body tr:last-child .col-1,
.review-screen .learning-objective-body tr:last-child .col-2,
.review-screen .learning-objective-body tr:last-child .col-3,
.review-screen .learning-objective-body tr:last-child .col-4 {
  border: 0 none;
}
.marking-scheme-input {
  cursor: pointer;
  border: 12px solid #FFF;
  box-shadow: 0 0 0 1px rgba(1, 1, 1, 0.125);
  border-radius: 24px;
  display: block;
  height: 12px;
  width: 12px;
  margin: auto;
  position: relative;
}
.marking-scheme-input:after {
  content: " ";
  display: none;
  border-radius: 10px;
  position: absolute;
  top: -9px;
  left: -9px;
  border: 9px solid currentColor;
}
.marking-scheme-input:not(.active):hover:after {
  display: block;
  opacity: 0.5;
}
.marking-scheme-input.active:after {
  display: block;
}
.ps-container>.ps-scrollbar-y-rail>.ps-scrollbar-y {
  background-color: #343434;
}
.ps-container:hover>.ps-scrollbar-y-rail:hover>.ps-scrollbar-y {
  background-color: #343434;
}
.ps-container.ps-in-scrolling.ps-y>.ps-scrollbar-y-rail>.ps-scrollbar-y {
  background-color: #343434;
}

$('.learning-objective-header').scrollToFixed();

http://codepen.io/2ne/pen/3bc99200b9d501c329608a153f63fde6

1 个答案:

答案 0 :(得分:1)

我不认为该插件提供了一种处理相对固定定位的方法。但是,它确实提供了根据屏幕顶部和底部的位置绝对定位固定元素的选项。因此,如果你知道固定元素上方区域的高度,你可以简单地使用marginTop选项来设置位置,如下所示:

$(document).ready(function() {
  $('.learning-objective-header').scrollToFixed({
    marginTop: 137
  });
});

这里有一个修改过的Codepen:http://codepen.io/pen/rVpqpz