jQuery、JavaScript、HTML:加载其他内容后如何加载图像?
我有一个非常复杂的页面,有很多脚本和相当长的加载时间。在该页面的顶部,我想实现 jquery Nivo Slider (http://nivo.dev7studios.com/)。
在文档中,它说我必须列出 div#slider 内滑块的所有图像
<div id="slider">
<img src="images/slide1.jpg" alt="" />
<a href="http://dev7studios.com"><img src="images/slide2.jpg" alt="" title="#htmlcaption" /></a>
<img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
<img src="images/slide4.jpg" alt="" />
</div>
,但是我可能有 10 个 1000x400px 的图像,这是相当大的。这些图像将在页面加载时加载。由于它们在我的标题中,这可能需要相当长的时间。
我正在寻找一种使用任何 jquery 滑块插件(如 nivo 滑块)的方法,但要么动态加载图像,要么在页面上的其他所有内容加载后加载所有这些图像。
知道我该如何解决这个问题吗?
是否有办法在页面上的所有其他内容加载后启动 JavaScript 进程?如果有办法我可能会解决我的问题(使用 jquery ajax load() 方法)...但是我不知道如何等待其他所有内容加载,然后启动包含所有图像的滑块。
I have a very complex page with a lot of scripts and a rather long loading time. On top of that page I want to implement the jquery Nivo Slider (http://nivo.dev7studios.com/).
In the documentation it says I have to list all images for the slider inside of a div#slider
<div id="slider">
<img src="images/slide1.jpg" alt="" />
<a href="http://dev7studios.com"><img src="images/slide2.jpg" alt="" title="#htmlcaption" /></a>
<img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
<img src="images/slide4.jpg" alt="" />
</div>
However I might have 10 images with a 1000x400px which is quite big. Those images would load when the page loads. Since they are in my header this might take quite a while.
I looking for a way to use any jquery Slider Plugin (like the nivo slider) but either dynamically load images or load all those images after everything else on my page has loaded.
Any idea how I could solve that?
Is there even a way to start a javascript process after everything else on the page has loaded? If there is a way I might have an solution for my problem (using the jquery ajax load() method) ... However I have no idea how to wait for everything else to load and then start the slider with all the images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是我们所做的,而且效果很好。我们跳过了设置
img
的src
属性,并将 img-location 添加到了假属性lsrc
中。然后我们加载一个具有lsrc
值的动态图像,并仅在加载后设置实际图像的src
。它不是为了加快加载速度,而是仅在图像完全下载到页面上时才显示图像,这样用户就不必看到令人讨厌的半加载图像。加载实际图像时可以使用占位符图像。
这是代码。
编辑:技巧是仅当源加载到临时 img 中时才设置 src 属性。
$(img).load(fn);
处理这个问题。Here's what we did and its working great. We skipped setting
src
attribute ofimg
and added img-location to a fake attributelsrc
. Then we load a dynamic image withlsrc
value, and set thesrc
of actual image only after its loaded.Its not about faster loading, but its about showing the images only when its downloaded completely on your page, so that user do not have to see that annoying half-loaded images. A placeholder-image can be used while the actual images are being loaded.
Here's the code.
Edit: Trick is to set the
src
attribute only when that source is loaded in temporary img.$(img).load(fn);
handles that.除了 Xhalent 的答案之外,还可以使用 jQuery 中的 .append() 函数将它们添加到 DOM 中:
您的 HTML 将具有:
然后您的 jquery 将是:
In addition to Xhalent's answer, use the .append() function in jQuery to add them to the DOM:
Your HTML would just have:
And then your jquery would be:
查看 jquery load() 事件,它会等待所有内容,包括
加载图形,然后您可以加载图像使用:
您也可以向图像添加 onload 函数
check out jquery load() event, it waits for everything including graphics
on load you could then load the images using:
You can add a function onload to the image too
我正在使用下面的内容来为我的滑块提供动力并提高页面加载性能。
I am using the below to power my slider and improve the page load performance.
最好的使用方法是b -lazy js。
bLazy 是一个轻量级延迟加载图像脚本(压缩和压缩后小于 1.2KB)。它允许您延迟加载和多重服务您的图像,以便您可以节省带宽和服务器请求。如果用户不浏览整个页面,他/她将获得更快的加载时间并保存加载的数据。
有关选项、函数和示例的完整列表,请访问博客文章:http://dinbror.dk/blog/炽热。
以下示例是带有图像回调的延迟加载多服务响应式图像示例:) 如果您的设备宽度小于 420 像素,它将提供更轻、更小的图像版本。当图像加载完毕后,它会在回调中删除加载器。
在 Html 中
在 js
示例
the best way to use is b -lazy js.
bLazy is a lightweight lazy loading image script (less than 1.2KB minified and gzipped). It lets you lazy load and multi-serve your images so you can save bandwidth and server requests. The user will have faster load times and save data loaded if he/she doesn't browse the whole page.
For a full list of options, functions and examples go to the blog post: http://dinbror.dk/blog/blazy.
The following example is a lazy loading multi-serving responsive images example with a image callback :) If your device width is smaller than 420 px it'll serve a lighter and smaller version of the image. When an image has loaded it removes the loader in the callback.
In Html
In js
Example
jquery 有一个在文档加载后执行 javascript 的语法:
jquery has a syntax for executing javascript after document has loaded: