同时浏览当前页面preLOAD第二页页面、preLOAD

2023-09-10 16:58:08 作者:烟火情味

比方说我的网站有5页的内容。是否有可能以preLOAD,而访问者查看第一页下两页。所以,当他们点击链接到第二或第三页时,它会立即出现。

Let's say my website has 5 pages of content. Is it possible to preload the next two pages while visitor is viewing the first page. So that when they click the link to 2nd or 3rd page, it will appear immediately.

推荐答案

当然,你可以有一些invisble容器是这样的:

Sure, you can have some invisble containers like this:

HTML

<div id="page">
  <a href="page2.html" id="page2-link">Go second page</a>
</div>
<div id="page2">
</div>

CSS

#page2 { display:none }

Javascript的

然后,当在JavaScript的preLOAD后,第二页已经被加载,并把它放在invisble容器:

Then when at the javascript preload the second page after it has been loaded and put it on the invisble container:

$().ready(function(){
  $('#page2').load('page2.html #page');
});

和链接被点击的时候就显示出隐藏的容器中选择页,并删除其他,或者只是隐藏当用户​​想要返回或它不会再次加载的东西。

And when the link is clicked just show the hidden container with selected page and remove the other, or just hide it to not load again when user wants to return or something.

$().ready(function(){
  $('#page2-link').on('click',function(){
       $('#page').html( $('#page').html() ); //this will replace html content
  });
});