简体   繁体   English

在页面加载时使用默认图像,然后覆盖它

[英]Using a default image on load of the page, then overwrites it

I have a page which contains several images.我有一个包含多个图像的页面。 When the page loads it provides a default image for each IMG:当页面加载时,它会为每个 IMG 提供一个默认图像:

import default from './pics/default.jpg';

<img src={default} alt="#"/>

What I want is something like this:我想要的是这样的:

<img src1={default} src2="img url" alt="#"/>

Where src2 overwrites src1 when it is loaded. src2 在加载时会覆盖 src1。

Edit: The first image (src1) comes as the page loads while the second (src2) is requested trough the internet.编辑:第一个图像(src1)在页面加载时出现,而第二个图像(src2)是通过互联网请求的。 Example : https://cdn.pixabay.com/photo/2016/10/17/10/52/wind-farm-1747331__340.jpg示例https ://cdn.pixabay.com/photo/2016/10/17/10/52/wind-farm-1747331__340.jpg

Use the onLoad event on your <img> element.<img>元素上使用onLoad事件。 You can give it a display: none style until it finishes loading.你可以给它一个display: none样式,直到它完成加载。

 class ImageRoll extends React.Component { state = {loaded: false}; showImage = () => { this.setState({loaded: true}); } render() { return ( <> <img src="/path-to-initial" style={ this.state.loaded ? {display: "none"} : {}} /> <img src="/path-to-seocndary" onLoad={this.showImage} style={ this.state.loaded ? {} : {display: "none"}} /> </> ); } }

Edit: As opposed to the other answers, the reason we want to do this is that replacing the src on an image is actually bad practice.编辑:与其他答案相反,我们想要这样做的原因是替换图像上的 src 实际上是不好的做法。 If you want to roll an image it is better to have two <img> elements and toggle between them depending on conditions.如果要滚动图像,最好有两个<img>元素并根据条件在它们之间切换。

You can just update directly the src of the image, I have a sample on Code Sandbox .您可以直接更新图像的 src,我在Code Sandbox上有一个示例。

This is the content:这是内容:

function App() {
  const [imageSrc, setImageSrc] = React.useState("/placeholder.jpg");

  function loadImage() {
    setImageSrc(`https://picsum.photos/200?${Date.now()}`);
  }

  return (
    <div className="App">
      <div>
        <img alt="small-image" src={imageSrc} />
      </div>
      <div>
        <button onClick={loadImage}>Load Image!</button>
      </div>
    </div>
  );
}

This is just a basic example, you can implement your own logic.这只是一个基本示例,您可以实现自己的逻辑。 Hope this helps!希望这可以帮助!

As I understand the situation, you want to load the first image with the page then after the page has loaded, replace the first image with an image from another source.据我了解,您想在页面加载第一张图片,然后在页面加载后,将第一张图片替换为来自其他来源的图片。

In the example below the default image is " https://via.placeholder.com/100 ", it is displayed as the page loads.在下面的示例中,默认图像是“ https://via.placeholder.com/100 ”,它在页面加载时显示。 The second image is " https://via.placeholder.com/300 ", and is displayed after page load is complete.第二张图片是“ https://via.placeholder.com/300 ”,页面加载完成后显示。

Set up all image tags by adding the attribute data-src="secondimage.jpg" in the image tag.通过在图像标签中添加属性data-src="secondimage.jpg"来设置所有图像标签。

How this works : After page load is complete, the script loops over all img tags checking for the data-src attribute.这是如何工作的:页面加载完成后,脚本会遍历所有 img 标签,检查 data-src 属性。 If an img tag has a data-src attribute it replaces the src image with the address in data-src attribute.如果 img 标记具有 data-src 属性,它会将 src 图像替换为 data-src 属性中的地址。

If you want to pre-load the external secondary images just add new Image().src = "secondimage.jpg";如果要预加载外部辅助图像,只需添加new Image().src = "secondimage.jpg"; to your script.到你的脚本。

If you press Run code snippet you will see the image shows 300 indicating the second image has replaced the first default one.如果您按运行代码片段,您将看到图像显示 300,表示第二个图像已替换第一个默认图像。 See the commented section below for pre-loading secondary images if desired.如果需要,请参阅下面的评论部分以预加载辅助图像。

 /* uncomment if you want to pre-load the secondary images new Image().src = 'https://via.placeholder.com/300'; new Image().src = 'https://via.placeholder.com/400'; new Image().src = 'https://via.placeholder.com/500'; ... as many as necessary */ window.onload = function() { imgs = document.querySelectorAll('img'); for (i = 0; i < imgs.length; i++) { if (imgs[i].getAttribute('data-src')) { imgs[i].setAttribute('src', imgs[i].getAttribute('data-src')); } } }
 <img data-src="https://via.placeholder.com/300" src="https://via.placeholder.com/100">

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

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