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.

            
              html * {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #1EAAF1;
}

.progress-bar {
  height: 10px;
  width: 100%;
  background: rgba(255,255,255,0.3);
  margin-bottom: 10px;
}

.progress-bar .progress {
  background: #126690;
  height: 100%;
  width: 0;
}

img {
  max-width: 100%;
  max-height: 100%;
  border: solid 5px white;
  display: block;
}

.img.lazy-img-loaded > div {
  width: 100%;
  height: 100%;
  background-size: 150% auto;
  background-repeat: no-repeat; 
  background-position: center;
  position: relative;
  overflow: hidden;
  border: solid 10px rgba(255,255,255,0.5);
}

.img.lazy-img-loading:before {
  position: absolute;
  content: '';
  border: 10px solid rgba(255,255,255,0.3);
  border-top: 10px solid white;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  animation: Spin 2s linear infinite;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}

.img {
  width: 33.333%;
  height: 200px;
  text-align: center;
  padding: 1%;
  float: left;
  position: relative;
  perspective: 300px;
}

.img.lazy-img-loaded > div {
  animation: ImageLoaded 1s ease-in forwards;  
}

@keyframes Spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}


@keyframes ImageLoaded {
  0% {
    opacity: 0;
    transform: scale(0.8) rotateX(20deg);
  }
}

@media all and (max-width: 750px) {
  .img {
    width: 50%;
    height: 250px;
  }
}

@media all and (max-width: 550px) {
  .img {
    width: 100%;
    height: 300px;
  }
}
            
          
!
            
              /**
 * Simple React Lazy Image Loading Component
 * A very simple port of Otho.js for React
 */
class LazyImage extends React.Component {
  constructor(props) {
    super(props)
    let imgState = this
      ._getImageState(this.props.placeholderClass, this.props.placeholder)
    this.state = imgState
  }
  /**
   * @function
   * When the component mounts, check whether
   * to load the placeholder before requesting
   * to load the image source.
   */
  componentDidMount() {
    if (this.props.loadPlaceholder) {
      return this._imageLoad(
        this.props.placeholder,
        this._initialise.bind(this) 
      )
    }
    this._initialise()
  }
  /**
   * @function
   * Initialise the request of the image source.
   */
  _initialise() {
    this._imageLoad(
      this.props.src, 
      this._imageLoaded.bind(this), 
      this._imageError.bind(this)
    )
  }
  /**
   * @function
   * Create an image and attach listeners to
   * react to the status of the image load.
   * @params {String} - The image source
   * @params {Function|undefined} - Callback to execute on load
   * @params {Function|undefined} - Callback to execute on error
   */
  _imageLoad(src, loaded = () => {}, error = () => {}) {
     let image = new Image()
    image.onload = loaded
    image.onerror = error
    image.src = src
  }
  /**
   * @function
   * Handle when the image loads
   * @params {Object} - The event
   */
  _imageLoaded(event) {
    let imgState = this
      ._getImageState(this.props.loadedClass, this.props.src)
    
    this.setState(imgState)
    this.props.onImageLoad && this.props.onImageLoad()
  }
  /**
   * @function
   * Handle when the image fails to loads
   * @params {Object} - The event
   */
  _imageError(event) {
    let imgState = this
      ._getImageState(this.props.errorClass, this.props.error)
    
    this.setState(imgState)
    this.props.onImageError && this.props.onImageError()
  }
  /**
   * @function
   * Construct and get the new image state 
   * @params {String} - The specified image class(es)
   * @params {String} - The existing image class(es)
   * @returns {Object} - The new image state
   */
  _getImageState(currentClass, imageSrc) {
    return {
      applyClass: [ this.props.class, currentClass ],
      image: imageSrc
    }
  }
  render() {
    /**
     * Check the type of the lazy image and 
     * load the markup depending on whether
     * the image is a background image or 
     * generic image.
     */
    switch(this.props.type) {
      case 'background':
        let backgroundImage = { 'backgroundImage': `url(${this.state.image})` }
        return (
          <div className={this.state.applyClass.join(' ')}>
            <div style={ backgroundImage }></div>
          </div>
        )
      default:
        return (
          <div className={this.state.applyClass.join(' ')}>
            <img src={this.state.image}></img>
          </div>
        )    
    }
  }
}

function ProgressBar(props) {
  let progress = ( props.loaded / props.total ) * 100
  let progressStyle = { width: `${progress}%` }
  return (
    <div className="progress-bar">
      <div className="progress" style={progressStyle}></div>
    </div>
  )
}

class App extends React.Component {
  constructor(props) {
    super(props)
    
    const images = this._randomImages(21)
    
    this.state = {
      images,
      loaded: 0,
      total: images.length
    }
  }
  _randomImages(numImages) {
    
    function getRandom(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    let kittenImages = []
    
    for (let i = 0; i < numImages; i++) {
      let height = getRandom(900, 1000)
      let width = getRandom(1500, 1800)
      kittenImages.push(`http://placekitten.com/g/${width}/${height}`)
    }
    
    return kittenImages
  }
  _imageLoaded(image) {
    this.setState({
      loaded: this.state.loaded + 1
    })
  }
  render() {
    return (
      <div>
        <ProgressBar loaded={this.state.loaded} total={this.state.total}/>
        { 
          this.state.images.map((image) => {
            return (
              <LazyImage
                type="background"
                class="img"
                src={image}
                error="http://placehold.it/20x20"
                loadedClass="lazy-img-loaded"
                placeholderClass="lazy-img-loading"
                errorClass="lazy-img-error"
                onImageLoad={ () => this._imageLoaded(image) }
              />
            )
          }) 
        }
      </div>
    )
  }
}

ReactDOM.render(
  <App />, 
  document.getElementById('app')
)
            
          
!
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