简体   繁体   English

程序中PHP页面上的数据库中的“Lazyloading”信息

[英]“Lazyloading” information from a database on a PHP page in procedural

I know proper lazy loading usually requires using objects and a proxy pattern. 我知道正确的延迟加载通常需要使用对象和代理模式。 I'm not there yet as a site needs some redesign. 我不在那里因为网站需要重新设计。 However, is there any way I can still spool out DB data to a page and speed up loading time a bit? 但是,有什么办法可以将DB数据移到页面上并加快加载时间吗?

You can use JavaScript to start loading extra data as soon as the main page loads. 主页加载后,您可以使用JavaScript开始加载额外数据。 This way your main page can just outline the components on the page, present "spinners" indicating that more data is being loaded and then offload it to JavaScript to get more data. 这样,您的主页面可以概述页面上的组件,显示“微调器”,表示正在加载更多数据,然后将其卸载到JavaScript以获取更多数据。

This can be achieved with AJAX. 这可以通过AJAX实现。

When you first load a page, put place-holders where the heavy content is supposed to go. 当您第一次加载页面时,将占位符放在重要内容应该放置的位置。 For example, use a div with a distinctive style and an animated loading image inside so visitors can clearly tell there is some more loading going on. 例如,使用具有独特风格的div和内部的动画加载图像,以便访问者可以清楚地看出还有更多的加载正在进行中。 You can generate familiar looking AJAX loading images at ajaxload.info . 您可以在ajaxload.info上生成熟悉的AJAX加载图像。

Have some script at the bottom of your page that calls or binds the necessary functions to load the additional content using AJAX. 在页面底部放置一些脚本,调用或绑定必要的函数以使用AJAX加载其他内容。 You could bind the AJAX call to jQuery's .ready() for example to have the loading start as soon as the rest of the document is ready or .scroll() to start when the user scrolls over a certain element (eg past certain 'landmarks' on your page that separate blocks of content). 您可以将AJAX调用绑定到jQuery的.ready() ,例如,一旦文档的其余部分准备好就开始加载,或者当用户滚动某个元素时(例如,过去的某些'地标.scroll() ,启动.scroll() '在您的页面上分隔内容块)。

When you receive the AJAX response that contains the additional content (preferably pre-formatted as HTML in string form) replace the contents of the placeholder div with the AJAX response using .innerHTML = or jQuery's .html() . 当您收到包含其他内容的AJAX响应(最好预先格式化为字符串形式的HTML)时,使用.innerHTML =或jQuery的.html()替换占位符div的内容和AJAX响应。 Change the style of the div if necessary using setAttribute("class","newClass") or jQuery's .attr("class","value") to replace it's CSS class. 如果需要,使用setAttribute("class","newClass")或jQuery的.attr("class","value")来更改div的样式以替换它的CSS类。

The visitor will be able to see your full page and scroll through it while the loading is taking place. 访问者将能够看到您的整页并在加载过程中滚动浏览。 The additional content will appear when it's ready without interrupting the visitor's browsing experience. 附加内容将在准备就绪时显示,而不会中断访问者的浏览体验。 You can make this a little nicer for the eye by animating the transitions after the content injection. 通过在内容注入后设置过渡动画,您可以使眼睛更好一些。

If you need some additional examples of how to use AJAX to fetch content asynchronously have a look at these for the classical JavaScript approach or jQuery .ajax() docs . 如果您需要关于如何使用AJAX来获取内容的一些额外的例子异步看看这些为古典JavaScript方式或jQuery的阿贾克斯()文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM