Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Section Sticky Buttons</title>
</head>

<body>
  <section id="header">
    header
  </section>
  <section>
    hero
  </section>
  <section>
    section
  </section>
  <section id="events">
    <div>section</div>
    <div class="show">1</div>
    <div class="show">2</div>
    <div class="show">3</div>
    <div id="stickme-parent">
      <div id="stickme">
        Section Sticky Buttons »
      </div>
      <div class="show">4</div>
      <div class="show">5</div>
      <div class="show">6</div>
      <div class="show">7</div>
      <div class="show">8</div>
      <div class="show">9</div>
    </div>
  </section>
  <section>
    section
  </section>
  <section>
    section
  </section>
  <section>
    section
  </section>
  <section>
    footer
  </section>
</body>

</html>
              
            
!

CSS

              
                body {
  padding: 37px 0 0 0;
  margin: 0;
}
section {
  text-align: center;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  padding: 100px 0;
  border-bottom: 1px solid white;
  background: #12c2e9;
  background: -webkit-linear-gradient(30deg, #4f00bc, #29abe2);
  background: linear-gradient(30deg, #4f00bc, #29abe2);
  color: rgba(255, 255, 255, 0.5);
}
#header {
  padding: 10px 0;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 99;
}
#events {
  overflow: hidden;
  position: relative;
}
#events .show {
  width: 30.3%;
  background: white;
  float: left;
  padding: 150px 0;
  margin: 20px 1.5%;
  color: #29abe2;
}
#stickme-parent {
  clear: both;
  overflow: hidden;
  position: relative;
  display: block;
  padding-bottom: 70px;
}
#stickme {
  background: #ff416c;
  background: -webkit-linear-gradient(to bottom, #ff416c, #ff4b2b);
  background: linear-gradient(to bottom, #ff416c, #ff4b2b);
  width: 100%;
  padding: 10px 0;
  position: absolute;
  top: 0;
  color: white;
  font-weight: bold;
  font-size: 1.2em;
}

#stickme.stick {
  position: fixed;
  bottom: 0;
  top: auto;
}
#stickme.stick--is-stuck {
  position: absolute;
  bottom: 0;
}

              
            
!

JS

              
                (function() {
  "use strict";

  stick("#stickme", "bottom");

  function stick(ele, position = "top") {
    // get the element to be stuck
    const stick = document.querySelector(ele);
    const stickPos = stick.getBoundingClientRect();
    const stickBottom = stickPos.top + stick.clientHeight;

    // get the parent
    const stickParent = stick.parentNode;
    const stickParentPos = stickParent.getBoundingClientRect();

    // on debounce scroll
    window.addEventListener(
      "scroll",
      debounce(function() {
        // get current scroll pos
        let scrollPos =
          document.documentElement.scrollTop || document.body.scrollTop;
        
        // @TODO factor in `position`

        // current viewport dimentions
        let viewportHeight = document.documentElement.clientHeight;
        let viewportBottom = scrollPos + viewportHeight;

        // rang to stick
        if (viewportBottom > stickBottom) {
          stick.classList.add("stick");
        } else {
          stick.classList.remove("stick");
        }

        // if the bottom of the viewport is past the sticky element's parent bottom
        // anchor to bottom of parent
        if (viewportBottom > stickParentPos.top + stickParentPos.height) {
          stick.classList.add("stick--is-stuck");
        } else {
          stick.classList.remove("stick--is-stuck");
        }
      })
    );

    // slow yo role
    function debounce(func, wait = 5, immediate = true) {
      var timeout;
      return function() {
        var context = this,
          args = arguments;
        var later = function() {
          timeout = null;
          if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
      };
    }
  }
})();

              
            
!
999px

Console