/
index.html
121 lines (108 loc) · 2.5 KB
/
index.html
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>唐诗三百首</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
max-width: 750px;
margin: 0 auto;
text-align: center;
padding: 20px;
}
.content {
margin-top: 20px;
}
.content p {
line-height: 2;
font-size: 20px;
font-weight: bolder;
}
.btn {
margin-top: 20px;
text-align: right;
position: fixed;
bottom: 10vh;
left: 0;
right: 20px;
}
.btn button {
padding: 10px 20px;
border-radius: 5px;
border: 1px solid #ccc;
background: #3366ff;
color: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
<script>
init()
function init() {
getJSon();
}
// 随机获取1-321的整数
function getRandom() {
return Math.floor(Math.random() * 321 + 1);
}
// 获取json数据
function getJSon() {
const index = getRandom();
fetch(`./json/${index}.json`).then(res => res.json()).then(res => {
console.log(res);
// if (res.content.length !== 4) {
// return getJSon();
// }
const box = document.querySelector('.box');
box.innerHTML = `
<h1>${res.title}</h1>
<h3>${res.auth}</h3>
<h6>${res.type}</h6>
<div class="content">
${res.content.map(item => {
return `<p>${item}</p>`
}).join('')
}
</div >
<div class="btn">
<button onclick="getJSon()">下一个</button>
</div>
<div>
${res.desc}
</div>
`
scrollToTop();
})
}
// 根据内容,随机打乱顺序,返回新的数组
function randomArr(arr) {
const newArr = [];
while (arr.length) {
const index = Math.floor(Math.random() * arr.length);
newArr.push(arr[index]);
arr.splice(index, 1);
}
return newArr;
}
// 切换题目 滚动条回到顶部
function scrollToTop() {
const timer = setInterval(() => {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
const ispeed = Math.floor(-scrollTop / 6);
if (scrollTop === 0) {
clearInterval(timer);
}
document.documentElement.scrollTop = document.body.scrollTop = scrollTop + ispeed;
}, 30)
}
</script>
</html>