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 id="app"></app>
              
            
!

CSS

              
                html, body
  height: 100%
 
body
  background: #333
  display: flex
  justify-content: center
  align-items: center
  font-family: Helvetica Neue

.container
  border: 2px solid #00f
 
h1
  border: 1px solid #0c5
  height: 30px
  width: 300px
  font-size: 1.5em
  color: #eee
  display: block
              
            
!

JS

              
                class InfiniteScroll extends React.Component {  
  state = {top: 0};
  
  onScroll = () => {
    const top = this.refs.container.scrollTop;
    // this is not strictly necessary, but reduces the number of updates
    if (Math.floor(this.state.top/this.props.height) != Math.floor(top/this.props.height))
      this.setState({top});
  };

  renderElements = (from, to) => {
    const elems = [];
    for (let i = from; i<to; i++) {
      elems.push(this.props.renderElement(this.props.elems[i], i));
    }
    return elems;
  };
  
  render() {
    // number of items that fit in view
    const count = Math.ceil(this.props.containerHeight/this.props.height);
    const prefix = Math.floor(this.state.top/this.props.height);
    const suffix = this.props.elems.length - count - prefix;
    const prefixHeight = prefix*this.props.height;
    const suffixHeight = suffix*this.props.height;
    const style = {
      height: this.props.containerHeight + 'px',
      overflowY: 'scroll',
      overflowX: 'hidden'
    };
    return (
      <div ref="container" className="container" style={style} onScroll={this.onScroll}>
        <div style={{height: prefixHeight + 'px'}}></div>
        {this.renderElements(prefix, prefix+count)}
        <div style={{height: suffixHeight + 'px'}}></div>
      </div>
    );
  }
}

const customElement = (i, key) => {
  return <h1 key={key}>hello world {i}</h1>;
};

let a = [];
for (let i = 0; i<500000; i++) a.push(i);


ReactDOM.render(<InfiniteScroll elems={a} renderElement={customElement} containerHeight={200} height={30}/>, document.getElementById('app'));
              
            
!
999px

Console