自动加载
自动加载就是符合某些条件时才加载某些图片。
现在好多的网站都用到了自动加载图片技术,比如淘宝,刚打开淘宝时,加载一部分图片,在你浏览的过程中,往下拉再加载一部分图片,再往下拉的时候再加载一部分图片。
代码实现
<style>
*{
box-sizing:border-box;
}
ol,ul{
list-style: none;
margin:0;
padding:0;
}
#list{
max-width:680px;
margin:0 auto;
display:flex;
flex-wrap:wrap;
justify-content:space-between;
}
#list li{
width:calc(33.333333% - 10px);
text-align:center;
}
.oneToOne{
padding-top:100%;
position:relative;
}
.oneToOne img{
position:absolute;
width:100%;
height:100%;
left:0;
top:0;
}
.loadMore{
text-align:center;
margin:16px 0;
}
</style>
<body>
<ol id="list">
</ol>
<div class="loadMore">
<button id="loadMoreButton">加载更多</button>
</div>
<script>
var loadingImage = new Image();
loadingImage.src="https://i.loli.net/2017/08/08/5989307b6c87b.gif";
let list = document.querySelector('#list');
for(var i=0;i < 9;i++){
let li = document.createElement('li');
let div = document.createElement('div');
div.className = 'oneToOne';
let image = document.createElement('img');
image.src = '//via.placeholder.com/200x200?text=' + Math.random().toFixed(6);
div.appendChild(image);
let p = document.createElement('p');
p.textContent = `这是第 ${i+1} 段话`;
li.appendChild(div);
li.appendChild(p);
list.appendChild(li)
}
let loadMoreButton = document.querySelector('#loadMoreButton');
let n =1;
let demand = false;
loadMoreButton.onclick = function(){
if(demand){return}
let request = new XMLHttpRequest();
request.open('GET',`./page-${n+1}.json`);
request.onerror = function(){
demand = false
};
request.onload =function(){
demand = false;
n +=1;
let response = request.responseText;
let data = JSON.parse(response);
for(var i=0;i < data.content.length;i++){
let liString = `
<li>
<div class="oneToOne">
<img data-xxx="${data.content[i].url}"> // data-xxx 代指src
</div>
<p>${data.content[i].text}</p>
</li>`;
list.insertAdjacentHTML('beforeend',liString)
}
if(data.hasNextPage === false){
loadMoreButton.disabled = true;
loadMoreButton.textContent ="看完了"
}
};
demand = true;
request.send()
};
window.onscroll = function(){
var doc = document.documentElement;
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
var clientHeight = doc.clientHeight;
var viewportOffset = loadMoreButton.getBoundingClientRect();
var buttonTop = viewportOffset.top;
if( buttonTop <= clientHeight){
loadMoreButton.click()
}
}
</script>
</body>
// JSON
{
"content":[
{"url":"//via.placeholder.com/200x200?text=0.145596","text":"这是第 10 段话"},
{"url":"//via.placeholder.com/200x200?text=0.186863","text":"这是第 11 段话"},
{"url":"//via.placeholder.com/200x200?text=0.102256","text":"这是第 12 段话"},
{"url":"//via.placeholder.com/200x200?text=0.156688","text":"这是第 13 段话"},
{"url":"//via.placeholder.com/200x200?text=0.120045","text":"这是第 14 段话"},
{"url":"//via.placeholder.com/200x200?text=0.132079","text":"这是第 15 段话"},
{"url":"//via.placeholder.com/200x200?text=0.158627","text":"这是第 16 段话"},
{"url":"//via.placeholder.com/200x200?text=0.132366","text":"这是第 17 段话"},
{"url":"//via.placeholder.com/200x200?text=0.146998","text":"这是第 18 段话"}
],
"hasNextPage":true
}
顺便学习了 Flex 布局,图片实现等比例缩放。
什么是懒加载
懒加载就是延迟加载图片或符合某些条件时才加载某些图片。
懒加载的原理是什么
页面中的img元素,如果没有src属性,浏览器就不会发出请求去下载图片,只有通过javascript设置了图片路径,浏览器才会发送请求。
懒加载的原理就是先在页面中把所有的图片统一使用一张占位图进行占位,把正真的路径存在元素的“data-xxx”属性里,要用的时候就取出来,再设置;
懒加载就是在自动加载的基础上,增加了下面的代码。
window.onscroll = function(){
var images =document.querySelectorAll('img[data-xxx]');
for (var i=0;i<images.length;i++){
var imagesviewportOffset =images[i] .getBoundingClientRect();
var imagesTop = imagesviewportOffset.top;
if( imagesTop <= clientHeight){
images[i].src = images[i].getAttribute('data-xxx');
images[i].removeAttribute('data-xxx')
}
}
}
什么是预加载
提前加载图片,当用户需要查看时可直接从本地缓存中渲染
预加载实现有两种方法
var loadingImage = new Image();
loadingImage.src="images/big.jpg";
<link rel="prefetch" href="images/big.jpg">
服务器
npm i -g http-server
http-server -c-1