-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (85 loc) · 2.75 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
database = $.ajax({
url: "database.json",//json文件位置,文件名
type: "GET",//请求方式为get
dataType: "json", //返回数据格式为json
async: false,
success: function (data) {//请求成功完成后要执行的方法
}
});
picsData = Object.values(database.responseJSON).reverse();
//Global vars:
var dataLength = picsData.length;
var loopLength = 6; //Set loop length.
var initItemsLength = 12; //set initial items length.
var itemDelay = 200;
//Create elements:
function getItem(param) {
//Find data:
var imgName = picsData[param].thumb_name;
//Create image:
var image = new Image();
image.className = 'img-item';
image.src = 'imgs/' + imgName + '.webp';
//Create title:
var title = document.createElement('span');
title.className = "thumb-title";
titleText = document.createTextNode(picsData[param].thumb_name);
title.appendChild(titleText);
//Create container:
var item = document.createElement('div');
item.className = 'item';
item.appendChild(title);
item.appendChild(image);
return item;
}
//Append initial items:
function appendItemInit() {
for (i = 0; i < initItemsLength; i++) {
(function (i) {
setTimeout(function () {
if (initItemsLength < dataLength) {
//Get item:
var elems = getItem(i);
//Create jQuery object:
var $elems = $(elems);
//Append item:
$(".grid").append($elems);
// console.log(picsData[i].id);
}
else {
$(".status").fadeIn();
};
}, itemDelay * i);
}(i)); //Add delay between appended items.
};
};
appendItemInit();
//Append new items:
function appendItem() {
for (i = 0; i < loopLength; i++) {
(function (i) {
setTimeout(function () {
if (initItemsLength < dataLength) {
//Get item:
var elems = getItem(initItemsLength);
//Create jQuery object:
var $elems = $(elems);
//Append item:
$(".grid").append($elems);
// console.log(picsData[initItemsLength].id);
//Modify items counter:
initItemsLength += 1;
}
else {
$(".status").fadeIn();
};
}, itemDelay * i);
}(i)); //Add delay between appended items.
};
};
//Infinite Scroll (trigger):
$(window).scroll(function () {
if (Math.ceil($(window).scrollTop() + window.innerHeight) == $(document).height()) {
appendItem();
}
});