预加载图像会减慢页面加载速度
我有一个内置 jquery 的幻灯片,它将图像 url 存储在数组中。当您单击下一步按钮时,它会向服务器发送请求以获取图像,然后显示它。这是可行的,但是一旦图像被检索,图像就会从上到下逐渐加载。我通过添加以下代码解决了这个问题...
//Preload background images
function MM_preloadImages() {
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
MM_preloadImages(<%= preloadImages %> );
现在的问题是第一个图像需要很长时间才能加载。我认为页面会加载第一个图像,然后在后台的预加载功能中加载其他图像?
感谢您的任何建议。
I have a slideshow built in jquery which stores image urls in an array. When you click on the next button it sends a request to the server to get the image, then show it. This works but the image load progressively from top to bottom once they are retreived. I solved this by adding the following code...
//Preload background images
function MM_preloadImages() {
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
MM_preloadImages(<%= preloadImages %> );
The problem now is the the first image takes ages to load. I though the page would load with the first image then the others in the preload function in the background?
Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我们无法从您发布的代码中判断预加载发生的顺序,但您可以将代码更改为仅在当前页面完成加载后运行 MM_preloadImages 。
也就是说,看起来您在一开始就预加载了所有图像。为什么不只预加载下一两张图像,并在每次幻灯片播放时继续执行此操作?这样,当用户尝试转到下一张幻灯片时,浏览器就不会仍然忙于尝试加载整个节目。
Well, we can't tell what order the preload happens from the code you posted, but you could change your code to only run MM_preloadImages after the current page has finished loading.
That said, it looks like you are preloading all the images at the start. Why not only preload the next image or two and keep doing that every time the slideshow advances? This way the browser isn't still busy trying to load the entire show while the user is trying to go to the next slide.