-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadMore.js
63 lines (62 loc) · 2.28 KB
/
loadMore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//预加载,让一定会显示的图片提前下载,例如菊花图
// let loadingImage = new Image()
// loadingImage.src = "https://media.giphy.com/media/y1ZBcOGOOtlpC/giphy.gif"
let button = document.querySelector('.button')
let olInsert = document.querySelector('.introduce > ol')
let n = 1
//标记作用,判断插入的data(JSON)是否为最后一张。
let hasNext = true
//标记作用,在一个请求未成功之前,不在发起另外一个请求。
let loadingImage = false
button.onclick = loadMore
function loadMore() {
if(! hasNext){return}
if(loadingImage){return}
let request = new XMLHttpRequest()
request.open('GET', `./JSON/page-${n}.html`)
//图片加载失败后。也会进行下一次请求。
request.onerror = function(){loadingImage = false}
request.onload = function () {
loadingImage = false
n += 1
let response = request.responseText
let data = window.JSON.parse(response)
for (var i = 0; i < data.content.length; i++) {
let liList = `
<li><img src="https://media.giphy.com/media/y1ZBcOGOOtlpC/giphy.gif" data-src=${data.content[i].url}><h3>${data.content[i].h3}</h3><p>${data.content[i].p}</p></li>
`
olInsert.insertAdjacentHTML('beforeend', liList)
}
if (data.hasNext === false) {
hasNext = false
// button.disabled = true
button.textContent = '没有更多了'
}
}
loadingImage = true
request.send()
}
window.onscroll = function () {
if(toUp(button)){
loadMore()
}
let images = document.querySelectorAll('img[data-src]')
for(let i=0;i<images.length;i++){
if(toUp(images[i])){
//不能直接用images[i].data-src赋值。
images[i].src = images[i].getAttribute('data-src')
//图片加载成功后,删除属性data-src,使下一次的images少一张。起着优化作用。
images[i].removeAttribute('data-src')
}
}
}
//判断一个element是否出现在页面里。(element从下到上)
function toUp(element){
let buttonTop = element.getBoundingClientRect().top
let viewHeight = document.documentElement.clientHeight
if (viewHeight > buttonTop + 60) {
return true
}else{
return false
}
}