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

前端 未结 6 2009
遥遥无期
遥遥无期 2020-12-07 08:28

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 stic

6条回答
  •  情深已故
    2020-12-07 09:29

    +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

    • github proyect
    • webpage
    • simple example in jsFiddle (same code as the snippet attached here)

    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;
    }
    
    
    

    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.

提交回复
热议问题