123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="container" class="container">
    <div class="header-floating">This is a floating header</div>

    <h2 class="header">Header 1</h2>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>

    <h2 class="header">Header 2</h2>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 2...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>

    <h2 class="header">Header 3</h2>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 3...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>

    <h2 class="header">Header 4</h2>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 4...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>

    <h2 class="header">Header 5</h2>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 5...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>
    <p class="message">Test record 1...</p>

</div>

            
          
!
            
              body {
  font: 14px/16px Arial, Verdana;
  padding: 10px;
  background: #345a80;
}

.container {
  width: 550px;
  margin: 0 auto;
  background: #dddddd;
  position: relative;
  overflow-y: auto;
  height: 300px;
  padding: 10px;
}

.header, .header-floating {
  font-size: 24px;
  color: #345a80;
  font-weight: normal;
  padding: 10px 0;
  margin: 0;
}

.header-floating {
  background: rgba(255, 255, 255, 0.9);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: none;
  padding-left: 10px;
  padding-right: 10px; 
}

.is-visible {
  display: block;
}
            
          
!
            
              window.onload = function() {
    // Select floating element. There's only one such element on the page, so we're using querySelector method
    // that selects only one element.
    var floatingTitle = document.querySelector('.header-floating');
    // Select all title elements. There are several elements, so we're using querySelectorAll method that selects all
    // elements available within a page
    var titles = document.querySelectorAll('.header');
    // Select a scrollable container
    var scrollable = document.querySelector('.container');
    // Add a function to run on a scroll event
    scrollable.onscroll = function() {
        changeFloatingHeaderOnScroll(floatingTitle, titles);
    };

    function changeFloatingHeaderOnScroll(floatingTitle, titles) {
        // Stop the execution if no titles found
        if (!titles.length) {
            return;
        }
        // Height of the scrollable element
        var elementHeight =  scrollable.offsetHeight;
        // Current scrolling position (is equal to 0 when the scrolling is on top). Everything above is hidden.
        var scrollTopPosition =  scrollable.scrollTop;
        // The last visible point at the bottom of the element. Everything below is hidden.
        var scrollBottomPosition = scrollTopPosition + elementHeight;
        // Elements for headers above and below current scrolling screen. We'll make use of them in case of no visible
        // headers on current screen.
        var lastAbove, lastBelow;

        // Indicator if fixed title is set. If it is, the iteration will be stopped.
        var fixedTitleIsSet;
        for (var i = 0, len = titles.length; i < len; i++) {
            if (!fixedTitleIsSet) {
                var title = titles[i];
                // Determine if the title is visible at the moment of scrolling
                var titleIsVisible = (title.offsetTop + title.offsetHeight) >= scrollTopPosition && title.offsetTop <= scrollBottomPosition;
                // Determine the last above title (above the scrolling point) and last below elements
                if (title.offsetTop <= scrollTopPosition) {
                    lastAbove = title;
                }
                else if (!lastBelow && title.offsetTop <= scrollTopPosition) {
                    lastBelow = title;
                }

                // If at the top of the element, hide the floating title.
                if (!scrollTopPosition) {
                    floatingTitle.style.display = 'none';
                    return;
                }

                // Set top value as the current scroll position
                floatingTitle.style.top = scrollTopPosition + 'px';

                // Now based on the determined title data, check different variations

                // If the title visible and is the first title, no floating title is needed
                if (titleIsVisible && i === 0) {
                    floatingTitle.style.display = 'none';
                    fixedTitleIsSet = true;
                }

                // If visible and there's a hidden header above, set the previous invisible header
                if (titleIsVisible && i > 0) {
                    floatingTitle.style.display = 'block';
                    floatingTitle.innerHTML = titles[(i - 1)].innerHTML;
                    fixedTitleIsSet = true;
                }

                // If invisible and the only element, set is as a floating title
                else if (!titleIsVisible && titles.length === 1){
                    floatingTitle.style.display = 'block';
                    floatingTitle.innerHTML = titles[i].innerHTML;
                    fixedTitleIsSet = true;
                }

                // If nothing is visible... set the last element above.
                else if (!titleIsVisible && titles.length === (i + 1) && lastAbove) {
                    floatingTitle.style.display = 'block';
                    floatingTitle.innerHTML = lastAbove.innerHTML;
                    fixedTitleIsSet = true;
                }

            }
        }
    }
};
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console