Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <section class="head">
    <p class="head-title">
        Header
    </p>
</section>

<section class="posts">
    
    <article data-section="1">
        <div class="post post--1">
            A
        </div>
    </article>
    
    <article data-section="2">
        <div class="post post--2">        
            B
        </div>
    </article>
    
    <article data-section="3">
        <div class="post post--3">                
            C
        </div>
    </article>
    
    <article data-section="4">
        <div class="post post--4">                
            D
        </div>
    </article>   
    
    <article data-section="5">
        <div class="post post--5">                
            E
        </div>
    </article>       
    
</section>
              
            
!

CSS

              
                .uncollapse--v() {
    // border-top: 0.01px solid transparent;
    // border-bottom: 0.01px solid transparent;
    
    &::before {  
        content: ' '; 
        display: table; 
    }    
}

html,
body {
    min-height: 100vh;
    margin: 0;
    
    background-color: #eee;
}

.head {
    padding: 3rem 2rem 1rem;
}

.head-title {
    margin: 0;
    
    font-size: 3rem;
    text-align: center;
}

.posts {
    
}

[data-section] {  
    margin: 0 auto;   
    
    .uncollapse--v;    
}

.post {
    position: relative;
    
    box-sizing: border-box;
    width: 80%;
    max-width: 32rem;     
    margin: 2rem auto;
    padding: 2rem;
    
    // top is in viewport
    [data-state-top='0'] & {

    }
    
    // top is above viewport
    [data-state-top='-1'][data-scrolling='false'] & {
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
    } 
    
    [data-state-bottom='0'][data-scrolling='true'] &,
    [data-state-bottom='-1'][data-scrolling='true'] &    {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
    }     
    
    // top is below viewport
    [data-state-top='1'] & {

    }     
}

.post--1 {
    height: 25vh;
    
    background-color: #fff;   
}

.post--2 {
    max-width: 36rem;    
    height: 125vh;  
   
    
    background-color: yellow;
    background: repeating-linear-gradient(
        45deg,
        yellow,
        yellow 10px,
        black 10px,
        black 20px
    );    
}

.post--3 {
    max-width: 24rem;        
    height: 50vh;
    
    background-color: red;
    
    // top is in viewport
    [data-state-top='0'] & {

    }
    
    // top is above viewport
    [data-state-top='-1'][data-scrolling='false'] & {
        position: relative;
        left: auto;
        right: auto;
        top: auto;
    } 
    
    [data-state-bottom='0'][data-scrolling] &,
    [data-state-bottom='-1'][data-scrolling] & {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;w
        
        margin: 2rem auto;
    }     
    
    // top is below viewport
    [data-state-top='1'] & {

    }       
}

.post--4 {
    max-width: 32rem;            
    height: 50vh;
    
    background-color: peachpuff;
}

.post--5 {
    max-width: 28rem;                
    height: 75vh;
    
    background-color: cyan;
}
              
            
!

JS

              
                /**
 * Sections
 */
Sections = ( function() {

    var settings = {
        offset: 0
    }

    var selector = {
        section: '[data-section]',
    }

    var cache = [];

    var init = function() {
        Debug.log( 'Sections.init()' );

        bindEventhandlers();

        onResize();
        onScroll();
    }

    var bindEventhandlers = function() {
        $( document )
            .on( 'viewport/scroll', function() {
                onScroll();
            } )
            .on( 'viewport/resize/finish', function() {
                onResize();
            } );

/*        
        $( selector.section ).imagesLoaded( function() {
            onResize();
            onScroll();
        } );
*/        
    }

    var onScroll = function() {
        var scrollTop = Viewport.get( 'scrollTop' );

        if( !cache ) {
            return false;
        }

        for( var i = 0; i < cache.length; i++ ) {
            // state top
            var stateTop = cache[i].state.top;
            if( cache[i].top <= scrollTop - settings.offset ) {
                cache[i].state.top = -1;
            } else {
                if( cache[i].top <= scrollTop + Viewport.get( 'height' ) - settings.offset ) {
                    cache[i].state.top = 0;
                } else {
                    cache[i].state.top = 1;
                }
            }

            if( stateTop !== cache[i].state.top ) {
                cache[i].section
                    .attr( 'data-state-top', cache[i].state.top );
            }

            // state bottom
            var stateBottom = cache[i].state.bottom;
            if( cache[i].bottom <= scrollTop - settings.offset ) {
                cache[i].state.bottom = -1;
            } else {
                if( cache[i].bottom <= scrollTop + Viewport.get( 'height' ) - settings.offset ) {
                    cache[i].state.bottom = 0;
                } else {
                    cache[i].state.bottom = 1;
                }
            }

            if( stateBottom !== cache[i].state.bottom ) {
                cache[i].section
                    .attr( 'data-state-bottom', cache[i].state.bottom );
            }
            
            cache[i].section.attr( 'data-scrolling', cache[i].scrolling );
        }
    }

    var onResize = function() {
        buildCache();

        onScroll();
    }

    var buildCache = function() {
        Debug.log( 'Sections.cache();' );

        cache = [];

        $( selector.section )
            .attr( 'data-state-top', '' )
            .attr( 'data-state-bottom', '' )
            .redraw()
            .attr( 'style', '' )
            .redraw();

        $( selector.section ).each( function() {
            var section = $( this );

            section
                .css( {
                    'height': 'auto'
                } )
                .redraw();

            var height = section
                // .find( '> *' )
                // .first()
                .outerHeight();

            if( !height ) {
                height = section
                    .find( '> *' )
                    .first()
                    .outerHeight( true );
            }

            section
                .css( {
                    'height': height + 'px'
                } )
                .redraw();

            var top = section.position().top;
            var bottom = top + height;

            cache.push( {
                id: section.attr( 'data-section' ),
                section: section,
                top: top,
                bottom: bottom,
                state: {
                    top: null,
                    bottom: null
                },
                scrolling: ( height > Viewport.get( 'height' ) )
            } );
        } );

        Debug.log( cache );

        $( document ).trigger( 'sections/buildcache' );
    }

    return {
        init: function() { init(); }
    }
} )();

$( document ).ready( function() {
    Sections.init();
} );




( function ( $ ) {
  $.fn.redraw = function() {
        return this.each( function() {
            var redraw = this.offsetHeight;
        } );
    };
}( jQuery ) );

              
            
!
999px

Console