css React Image Preloader

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了css React Image Preloader相关的知识,希望对你有一定的参考价值。

render() {
  const preloaderImage = '/path/to/preloader.gif';
  const heroImage = '/path/to/full_image.jpg';
  
  return (
    <div>
      <HeroImage srcPreload={preloaderImage} srcLoaded={heroImage} />
    </div>
    )
}
import React, { Component } from "react";
import PropTypes from "prop-types";

class HeroImage extends Component {

	constructor( props ) {
		super( props );
		this.fullImageSrc = null;
	}

	componentDidMount() {
		const fullImageLoader = new Image();
		fullImageLoader.src = this.props.srcLoaded;

		fullImageLoader.onload = () => {
			this.fullImageSrc.setAttribute( 'style', `background-image: url(${this.props.srcLoaded})` );
			this.fullImageSrc.classList.add( 'image-fade-in' );
		};
	}


	render() {
		const {
			srcPreload
		} = this.props;

		return (
			<section className="site-top-hero preload-image-container">
				<div className="image-loaded" ref={imageLoadedElem => this.fullImageSrc = imageLoadedElem} />
				<div className="image-preload" style={{"backgroundImage":`url(${srcPreload})`}} />
			</section>
		);
	}
}

export default HeroImage;

HeroImage.propTypes = {
  srcLoaded: PropTypes.string,
  srcPreload: PropTypes.string
}
.preload-image-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.image-preload {
  position: absolute;
  z-index: 1;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-size: cover;
}

.image-fade-in {
  opacity: 1;
}

.image-loaded {
  position: absolute;
  z-index: 2;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-size: cover;
  opacity: 0;
  transition: opacity 1000ms ease;
}

.image-fade-in {
  opacity: 1;
}

以上是关于css React Image Preloader的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript Image Preloader v1.0

在我的 php 文件解析和下载数据时调用 preloader(gif , loading image)

css preloader.css

css preloader.css

tflearn 数据集太大无法加载进内存问题?——使用image_preloader 或者是 hdf5 dataset to deal with that issue

如何覆盖 React 本机 web 预定义的 css