支持事件的图像预加载器 JavaScript
我正在尝试找到一个图像预加载脚本。
虽然我发现了一些,但它们都不支持预加载完成时触发的事件。
有谁知道有什么脚本或 jQuery 插件可以做到这一点吗?
希望这个问题适合 stackoverflow - 如果不适合,请立即将其删除。
I am trying to find an image preloader script.
While i found a few, none of them supports an event that is triggered when preloading is finished.
Does anyone know of any script or jQuery plugin that will do this?
Hope this question is appropriate for stackoverflow - if not, feel free to remove it in an instant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个函数,它将从数组中预加载图像,并在最后一个图像完成时调用回调:
由于我们现在处于使用 Promise 进行异步操作的时代,因此这是上述版本的一个版本,它使用 Promise 并通知调用者通过 ES6 标准承诺:
并且,这是使用 2015 jQuery 承诺的版本:
Here's a function that will preload images from an array and call your callback when the last one has finished:
And since we're now in the age of using promises for asynchronous operations, here's a version of the above that uses promises and notifies the caller via an ES6 standard promise:
And, here's a version using 2015 jQuery promises:
要获得更强大的解决方案,请考虑使用带有几个回调的
PRELOADER
函数 (jsFiddle )。保持简单:
在本示例中,我在
Object
文字PRELOADER_OBJECT
内传递回调和图像哈希,然后覆盖PRELOADER
内的回调:};
如果站点负载足够大,需要图像预加载器,则可以轻松修改模式文本显示以支持数据驱动的 jQuery 动画。
For a more robust solution, consider this
PRELOADER
function with a couple of callbacks (jsFiddle).Keeping it simple:
In this example, I'm passing callbacks and an image hash inside an
Object
literalPRELOADER_OBJECT
, then overriding the callbacks insidePRELOADER
:};
With a site load large enough to require an image preloader, the modal text display could be easily modified to support a data-driven jQuery animation.
预加载和加载是同一件事。您可以插入图像(创建新图像或更改现有图像的“src”属性),但使用
$("element").hide()
或类似的方法隐藏元素。在执行此操作之前,请附加一个加载事件处理程序,如下所示:Preloading and loading are the same thing. You can insert the image (either create a new one or change the "src" attribute of an existing one) but hide the element using
$("element").hide()
or something similar. Before you do this, attach a load event handler like so:预加载需要一些额外的工作,例如创建新的图像元素,监视它们是否全部加载,然后用 DOM 中的现有元素替换它们。但是,您可以直接在 DOM
![]()
元素上无限次地执行此操作,而无需替换它们。我们可以使用 Fetch API 来访问图像,等到它们在
promise.all()
中全部下载完毕,然后一次性替换图像的src
属性使用window.requestAnimationFrame()
在最合适的时间显示img
元素。在以下示例中,我刷新
img
元素的src
属性 10 次。根据延迟,我正在使用从 API 加载 4 个图像所需的时间。因此,一旦我们加载了所有图像,我就会立即通过递归调用相同的refreshImagesNTimes
函数来发出新请求。当然,您可以选择一次加载任意数量的图像 blob,并使用简单的
setTimeout
机制以精确的时间间隔分组显示它们。Preloading takes some extra work like creating new image elements, monitoring if they are all loaded and then replacing them with the existing ones in the DOM. However you may do this directly on the DOM
<img>
elements indefinitely many times without replacing them.We may use the Fetch API to access the images, wait until they are all downloaded within a
promise.all()
and then in one go just replace thesrc
attributes of theimg
elements at the most suitable time by usingwindow.requestAnimationFrame()
.In the following example I refresh the
src
attributes of theimg
elements 10 times. As per the delay, i am using the the time it takes up to load 4 images from an API. So once we have all images loaded i am immediately placing a new request by calling the samerefreshImagesNTimes
function recursively.You may of course chose to load as many image blobs as you like all at once and display them in groups in precise time intervals by using a simple
setTimeout
mechanism.