【问题标题】:How to change Multiple small-res image source with the actual high-res image after they have been loaded?加载后如何将多个小分辨率图像源更改为实际的高分辨率图像?
【发布时间】:2020-10-22 09:30:25
【问题描述】:

我正在开发一个网页,该网页在应用低分辨率图像后加载高分辨率图像,并将它们设置为最初加载的小型低分辨率图像。这样做是为了更快地加载较小的模糊图像并在完全加载后用高分辨率图像替换它们不模糊 /strong>。

我正在尝试使用 javascript 添加此功能,但被卡住了。特别是因为我是新手。这是我到目前为止所写的。任何帮助表示赞赏。 T

(function() {
  let image = document.images //all low-res blurred images of class "image"
  let big_image = document.getElementsByClassName("image")

  function loadImage() {
    for (let i = 0; i <= big_image.length; i++) {
      big_image.onload = function() {
        image[i].src = big_image[i].src
        image[i].className = "noBlur" //set class to noblur from image to remove the blur filter
      }
    }
  }

  setTimeout(function() {
    big_image[0].src = "mainPage/res/fireWatch.jpg" //actual high res image srcs
    big_image[1].src = "mainPage/res/mountainMan.jpg"
    big_image[2].src = "mainPage/res/pyramid.jpg"
    big_image[3].src = "mainPage/res/valley.jpg"
    big_image[4].src = "mainPage/res/valley.jpg"
    big_image[5].src = "mainPage/res/valley.jpg"
  }, 50)
  document.addEventListener('DOMContentLoaded', loadImage, false);
}());

inspiration

【问题讨论】:

  • 您要查找的内容称为渐进式图像加载,您可以在浏览Medium 网站时看到它。您可以按照本指南获得相同的“效果”:First | Second | Third
  • 你可以试试这样的jsfiddle.net/4jsp935L
  • 谢谢您,@CarloCorradini,我根据您的两个建议进行了研究,但最终使用了一个开源库。当然,您帮助获取了有关此主题的更多信息

标签: javascript html css ajax


【解决方案1】:

正如评论所暗示的,像medium 这样的渐进式图像加载可以通过使用来自 GitHub blurry-image-load 的简单开源文件来实现。阅读 readme.md 并将必要的文件添加到您的项目中并按照说明进行操作。

【讨论】:

  • 我建议您使用更健壮的库,例如vanilla-lazyload,并对每个卸载的图像应用模糊滤镜。
  • @CarloCorradini 感谢您的更多建议。我也试试这个。有兴趣可以帮我再解决一个problem
  • 完成了,希望我再次提供帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 2015-03-12
  • 2014-11-20
  • 2011-02-11
  • 1970-01-01
  • 2011-04-07
相关资源
最近更新 更多