简体   繁体   English

在create-react-app中预加载所有图片

[英]Preload all pictures in create-react-app

I use this to create card game. 我用这个来制作纸牌游戏。 Now pictures are loaded directly after the rendering of the card component with background-image. 现在,在使用background-image渲染卡片组件后,将直接加载图片。 Therefore sometimes there are friezes. 因此,有时会有带状装饰。 I want all the pictures to be loaded before showing the main screen using a preloader. 我希望在使用预加载器显示主屏幕之前加载所有图片。 Tell me please how to do this. 请告诉我如何做到这一点。 Thank you. 谢谢。

 import React from 'react'; import '../styles/Card.css'; const Card = (props) => { const cardClick = () => { if(props.status === 'unselected') { props.onCardClick(props.cardIndex); } }; return ( <div className={`card card-${props.cardName} card-${props.status}`} onClick={cardClick}> <div className="card-inner"> <div className="card-face card-front"></div> <div className="card-face card-back"></div> </div> </div> ); } export default Card; 
 /*Set background to cards*/ .card-0C .card-front { background: url('../images/cards/0C.png'); background-size: cover; } .card-0D .card-front { background: url('../images/cards/0D.png'); background-size: cover; } .card-0H .card-front { background: url('../images/cards/0H.png'); background-size: cover; } .card-0S .card-front { background: url('../images/cards/0S.png'); background-size: cover; } .card-2C .card-front { background: url('../images/cards/2C.png'); background-size: cover; } .card-2D .card-front { background: url('../images/cards/2D.png'); background-size: cover; } 

Well there are a few ways. 那么有几种方法。 I'm gonna try and do my best to explain what you can do. 我会尽力解释你能做些什么。

  1. Use html preload attribute . 使用html preload属性 reference here 这里参考

The preload value of the link element's rel attribute allows you to write declarative fetch requests in your HTML head, specifying resources that your pages will need very soon after loading, which you therefore want to start preloading early in the lifecycle of a page load, before the browser's main rendering machinery kicks in. This ensures that they are made available earlier and are less likely to block the page's first render, leading to performance improvements. link元素的rel属性的preload值允许您在HTML头中编写声明性提取请求,指定页面在加载后很快需要的资源,因此您希望在页面加载的生命周期的早期开始预加载,之后浏览器的主要渲染机制开始运作。这可确保它们更早可用,并且不太可能阻止页面的首次渲染,从而提高性能。 This article provides a basic guide to how preload works. 本文提供了有关预加载如何工作的基本指南。

<link rel="preload" href="style.css" as="style">
<img rel="preload" src="image.png" as="image" />

NOTE: you would have to ditch the background css usage. 注意:您将不得不放弃背景css使用。

  1. Use actual styled html for the cards. 使用实际样式的html作为卡片。

This is more complex way of doing what you want. 这是做你想做的更复杂的方式。 Dropping the images and using straight up html styled with css. 删除图像并使用直接向上html样式与CSS。 This is perfectly doable and adds the bonus of complex animations to the cards elements if you ever do so desire. 这是非常可行的,如果您有这样的愿望,可以将复杂动画的加值添加到卡元素中。

  1. Use SVGs 使用SVG

This one is very similar to the HTML option because you would have to use an svg-loader to make it go straight into your html. 这个与HTML选项非常相似,因为你必须使用svg-loader才能直接进入你的html。

  1. Declare all your images in a hidden div making sure that the browser has your images loaded always . 在隐藏的div中声明所有图像,确保浏览器始终加载图像 reference here 这里参考

This one is a bit meh but does the job nonetheless. 这个有点meh但是仍然可以做到这一点。

It consists of having a hidden div like so: 它包含一个隐藏的div,如下所示:

div#preload { display: none; }

Then your app would load images 然后你的应用程序将加载图像

// assuming you have the proper loaders configured
const cardOne = require("path/to/card1/img");
const cardTwo = require("path/to/card2/img");

...

render() {
  return (
    <div id="preload">
      <img src={cardOne} />
      <img src={cardTwo} />
      {/* etc... */}
    </div>
  );
}

This div would always be rendered ensuring the browser loaded the images on first contact and your app would have the images cached and you could use a background solution like shown in the link: 这个div将始终呈现,以确保浏览器在第一次接触时加载图像,并且您的应用程序将缓存图像,您可以使用链接中显示的后台解决方案:

#element_01 {
    background: url(path/image_01.png) no-repeat;
    display: none;
    }
#element_02 {
    background: url(path/image_02.png) no-repeat;
    display: none;
    }
#element_03 {
    background: url(path/image_03.png) no-repeat;
    display: none;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM