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

              
                <div class="scrollscreen">
	
</div>
              
            
!

CSS

              
                /*
 * Cosmetics
 */
.scrollscreen--track {
    /* width of track, 18px should cover default track on MacOS and Windows */
    width:18px;
    background:#bebec1;
}

.scrollscreen--slider {
    /* space between slider and track */
    padding:.25em;
}

.scrollscreen--slider::after {
    /* slider style */
    background:#5f6165;
    border-radius:9999em;
}


/**
 * Layout
 */
.scrollscreen	{
	position:relative;
}

.scrollscreen--track {
    position:absolute;
    right:0;
    top:0;
    height:100%;
    pointer-events:none;
}

*[dir='rtl'] .scrollscreen--track {
    left:0;
    right:auto;
}

.scrollscreen--slider {
    position:absolute;
    top:0;
    left:0;
    right:0;
    box-sizing:border-box;
    height:0;
    border:none;
    background:transparent;
    pointer-events:none;
}

.scrollscreen--slider::after {
    content:'';
    display:block;
    width:100%;
    height:100%;
}

.scrollscreen--content {
    position:relative;
    height:100%;
    overflow-x:hidden;
    overflow-y:auto;
}

.scrollscreen--content::-webkit-scrollbar {
    /* fix WebKit problems with MacOS automatically hiding scrollbars */
    -webkit-appearance: none;
}










/**
 * Presentational Styles
 */
.scrollscreen {
	width:24em;
	height:100%;
	max-height:calc(100% - 4em);
	background:#dcdcdf;
	border-radius:.5em;
	overflow:hidden;
}

h1 {
	margin:0;
}

main {
	padding:2em;
}

html, body {
	height:100%;
}

body {
	display:flex;
	justify-content:center;
	align-items:center;
	color:#222;
}
              
            
!

JS

              
                (() => {
    
    // should we register the wakey wakey fix 
    const wakeyFixRequired = 'CSS' in window && typeof CSS.supports === 'function' && !CSS.supports('-webkit-appearance', 'none');
    
    const ROOT_CLASS = 'scrollscreen';

    const createElement = (tag, name) => {
        const el = document.createElement(tag);
        el.className = `${ROOT_CLASS}--${name}`;
        return el;
    }

    const createScrollscreen = (root) => {

        const fragment = document.createDocumentFragment();
        [...root.childNodes].forEach(child => {
            fragment.appendChild(child);
        });
        
        const content = createElement('div', 'content');
        content.appendChild(fragment);
        root.appendChild(content);
        
        const track = createElement('div', 'track');
        root.appendChild(track);
        
        const slider = createElement('div', 'slider');
        track.appendChild(slider);
        
        let pendingFrame = null;
        
        const redraw = () => {
            
            cancelAnimationFrame(pendingFrame);
            
            pendingFrame = requestAnimationFrame(() => {
            
                const contentHeight = content.scrollHeight;
                const containerHeight = root.offsetHeight;
                const percentageVisible = containerHeight / contentHeight;
                const sliderHeight = percentageVisible * containerHeight;
                const percentageOffset = content.scrollTop / (contentHeight - containerHeight);
                const sliderOffset = percentageOffset * (containerHeight - sliderHeight);
                
                track.style.opacity = percentageVisible === 1 ? 0 : 1;
                
                slider.style.cssText = `
                    height: ${sliderHeight}px;
                    transform: translateY(${sliderOffset}px);
                `;
            });
            
        }
        
        // need to update if window is resized or if container is scrolled
        content.addEventListener('scroll', redraw);
        window.addEventListener('resize', redraw);
        
        // first redraw
        redraw();

        // if we don't need to do the wakey wakey fix we done
        if (!wakeyFixRequired) {
            return;
        }

        // the wakey wakey function quickly scrolls the contents so Firefox enables the scrollbar and the user can drag it
        const wakey = () => {
            requestAnimationFrame(() => {
                const offset = content.scrollTop;
                content.scrollTop = offset + 1;
                content.scrollTop = offset; 
            });
        }

        // wake up scrollbars on MacOS Firefox
        root.addEventListener('mouseenter', wakey);

        // trigger so it's drawn correctly for the first time
        wakey();
    }

    // create scrollies
    [...document.querySelectorAll(`.${ROOT_CLASS}`)].forEach(createScrollscreen);

})();
              
            
!
999px

Console