jQuery 为 iPhone 预加载图像
// After a page turn
after: function(opts) {
var currPageIndex = opts.curr - 1;
generatePage(currPageIndex); // This page
generatePage(currPageIndex + 1); // Right side page
generatePage(currPageIndex + 2);
generatePage(currPageIndex + 3);
}
});
});
// Generates the page
function generatePage(pageID){
// Check not already loaded
if(pageID >= 0 && pageID < arrPages.length && !arrPages[pageID][2])
{
arrPages[pageID][2] = true;
$('#page' + pageID).html('<img src="<%=strProjectPath%>/pages/originals/' + arrPages[pageID][1] + '" alt="Page image" />');
}
}
// Load first page
$(document).ready(function() {
generatePage(0);
generatePage(1);
generatePage(2);
});
正如您所看到的,当文档准备好后,第 0、1 和 2 页就生成了。 0 是立即可见的图像,然后 1 和 2 是在翻页时预加载的。
当他们翻页时,它会加载他们正在查看的当前两页(如果直接跳过那里并且没有预加载)和接下来的两页。
如何使加载偏向,以便它首先加载立即可见的页面?您可以使用 Jquery onload 对下一页进行排队吗?目前,理论上 4 个页面可以同时加载图像,这在移动设备上会很慢。
// After a page turn
after: function(opts) {
var currPageIndex = opts.curr - 1;
generatePage(currPageIndex); // This page
generatePage(currPageIndex + 1); // Right side page
generatePage(currPageIndex + 2);
generatePage(currPageIndex + 3);
}
});
});
// Generates the page
function generatePage(pageID){
// Check not already loaded
if(pageID >= 0 && pageID < arrPages.length && !arrPages[pageID][2])
{
arrPages[pageID][2] = true;
$('#page' + pageID).html('<img src="<%=strProjectPath%>/pages/originals/' + arrPages[pageID][1] + '" alt="Page image" />');
}
}
// Load first page
$(document).ready(function() {
generatePage(0);
generatePage(1);
generatePage(2);
});
As you can see, when the document is ready, page 0, 1 and 2 are generated. 0 is the immediately viewable image, then 1 and 2 are preloaded for when they turn the page.
When they turn the page, it loads the current two pages they are viewing (incase the skipped there directly and they aren't preloaded) and the next 2 pages.
How can you bias the loading, so it loads the immediately viewable pages first? Can you use Jquery onload to queue the next pages? At the moment 4 pages could theoretically all load their images simultaneously which would be slow on mobiles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,您可以使用
jQuery
load()
,但是您不应该立即添加所有图像。例如,您可以像这样将它们排队:Sure, you can use the
jQuery
load()
, but then you shouldn't add all the images immediately. You could, for example, queue them up like this: