jquerypositionfixedsidebarsticky

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


I have been looking for some time now for a solution to my sticky sidebar problem. I have a specific idea of how I would like it to act; effectively, I would like it to stick to the bottom as you scroll down, and then as soon as you scroll back up I would like it to stick to the top, in a fluid motion (no jumping). I am unable to find an example of what I am trying to achieve, so I have created an image that I hope will illustrate the point clearer:

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

  1. Sidebar sits under the header.
  2. As you scroll down the sidebar remains level with the content of the page so that you can scroll through both sidebar and content.
  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/ .


Solution

  • +1 to the very nice and ilustrative image.

    I know it's an old question, but I casually found the same question posted by you in forum.jquery.com and one answer there (by@tucker973), suggested one nice library to make this and wanted to share it here.

    It's called sticky-kit by @leafo

    Here you have the code of a very basic example that I prepared and a working demo to see the result.

    /*!
     * 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.