-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewsCard-nocodb.html
239 lines (175 loc) · 6.47 KB
/
NewsCard-nocodb.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>News Card</title>
<style>
/* body, html {
-webkit-overflow-scrolling: touch;
background-color: var(--color-bg-1);
font-family: -apple-system,BlinkMacSystemFont,PingFang SC,Hiragino Sans GB,HarmonyOS_Medium,Arial,Microsoft YaHei,Helvetica Neue,Helvetica,sans-serif;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
width: 100%;
} */
.card {
display: flex;
flex-direction: column;
align-items: center;
width: 460px;
height: 460px;
border: 1px solid #ccc;
border-radius: 15px;
padding: 20px;
box-shadow: 10px 10px 5px #8f8f8f96;
background-image: linear-gradient(0deg, #a8edea 0%, #fed6e3 100%);
}
.imagecss {
width: 100%;
height: 200px;
object-fit: cover;
margin-bottom: 10px;
border-radius: 15px;
box-shadow: 3px 3px 3px #8f8f8f96;
}
.title {
font-size: 18px;
font-weight: bold;
margin-bottom: 10px;
}
.content {
width: 100%;
height: 200px;
font-size: 16px;
margin-bottom: 20px;
}
#slider-container {
width: 100%;
height: 200px;
object-fit: cover;
margin-bottom: 20px;
border-radius: 15px;
}
</style>
</head>
<body>
<div class="card">
<div id="slider-container">
<img id="img">
</div>
<h2 class="title">标题</h2>
<p class="content" id="content"></p>
<p class="source"></p>
<p class="time"></p>
</div>
<script>
//卡片切换index的值
let swichlen = 0;
//api传过来的内容和配置
let apidata;
let setdata;
//卡片切换默认值 图片切换默认值
let cardupdatetime = 12000
let imgupdatetime = 4000
//标题和内容的文本字号
let titlefontsize = ''
let contentfontsize = ''
//背景颜色
let backgroundcolor = ''
//定义全局变量imgInterval
let imgInterval = null;
//获取配置信息
fetch('http://localhost:7088/fusion/v1/datasheets/dstjwuUV3ASAunlSct/records?viewId=viwYWZzbfMcFo&fieldKey=name', {
headers: {
'Authorization': 'Bearer uskXnNevhmJ3c1AI3RFeQN4'
}
})
.then(response => response.json())
.then(data => {
setdata = data;
console.log(setdata.data);
cardupdatetime = setdata.data.records[0].fields.cardupdatetime * 1000;
console.log(cardupdatetime);
imgupdatetime = setdata.data.records[0].fields.imgupdatetime * 1000;
console.log(imgupdatetime);
titlefontsize = setdata.data.records[0].fields.titlefontsize;
console.log(titlefontsize);
contentfontsize = setdata.data.records[0].fields.contentfontsize;
console.log(contentfontsize);
backgroundcolor = setdata.data.records[0].fields.backgroundcolor[0];;
console.log(backgroundcolor);
//设置标题内容的字号,背景颜色
document.querySelector('.title').style.fontSize = titlefontsize;
document.querySelector('.content').style.fontSize = contentfontsize;
document.querySelector('.card').style = backgroundcolor;
})
.catch(error => console.error(error));
const titleElement = document.querySelector('.title');
const contentElement = document.querySelector('.content');
const imgcontainer = document.getElementById("slider-container");
const timeElement = document.querySelector('.time');
const sourceElement = document.querySelector('.source');
// titleElement.style.fontSize = titlefontsize;
// document.querySelector('.content').style.fontSize = contentfontsize;
//图片轮播函数
function updateImg(imagesrc) {
let imglen = 0;
const imageElement = document.getElementById("img");
imageElement.src = "";
//imgcontainer.appendChild(imageElement);
imageElement.src = 'http://localhost:7088/'+ imagesrc[imglen].url;
imageElement.className = "imagecss"
//设置轮播时间和地址,bing保存定时器的返回值
imgInterval = setInterval(() => {
imglen = (imglen + 1) % imagesrc.length;
imageElement.src = 'http://localhost:7088/'+ imagesrc[imglen].url;
console.log(imageElement);
}, imgupdatetime);
}
//时间戳转换函数
function formatDate(timestamp) {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
function updateCard() {
// 设置标题和内容
titleElement.textContent = apidata.data.records[swichlen].fields.title;
contentElement.textContent = apidata.data.records[swichlen].fields.content;
sourceElement.textContent = apidata.data.records[swichlen].fields.source;
//设置时间并调用函数
const timestamp = apidata.data.records[swichlen].fields.time;
const formattedDate = formatDate(timestamp);
timeElement.textContent = formattedDate;
//清除图片轮播的定时器
clearInterval(imgInterval);
//设置图片并引用函数
const imagesrc = apidata.data.records[swichlen].fields.image
updateImg(imagesrc);
// 增加索引并在必要时循环回开头
swichlen = (swichlen + 1) % apidata.data.records.length;
}
//获取数据并更新
fetch('http://localhost:6688/api/v1/db/data/noco/p_slj851n5y3r1rg/%E6%96%B0%E9%97%BB%E5%8D%A1%E7%89%87/views/%E6%96%B0%E9%97%BB%E5%8D%A1%E7%89%87?offset=0&limit=25&where=', {
headers: {
'xc-auth': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Ijc5ODczNzcwQHFxLmNvbSIsImZpcnN0bmFtZSI6bnVsbCwibGFzdG5hbWUiOm51bGwsImlkIjoidXNfNzJxa2IwbXhzMGQxZ2ciLCJyb2xlcyI6Im9yZy1sZXZlbC1jcmVhdG9yLHN1cGVyIiwidG9rZW5fdmVyc2lvbiI6Ijg4ZDA2M2ZlNTIwYTJjNWI2YmVlNmI5MmEwMmJjZGYzNjYxM2NjZDcyMWEwOTM2ZWI4MWZlYzRhMDEyZGY1NDk0ZGFhNTRhOWVlYjBjNjg2IiwiaWF0IjoxNjgwMzM3NzA3LCJleHAiOjE2ODAzNzM3MDd9.pyDhtRvEdSRrfjsJXm7KsiBVL4Cp5JMPjuMaHvclG3g'
}
})
.then(response => response.json())
.then(data => {
apidata = data;
console.log(apidata.data);
// 使用数据更新卡片元素,10秒更新
updateCard();
//setInterval(updateCard, cardupdatetime);
setInterval(() => {
updateCard();
}, cardupdatetime);
})
.catch(error => console.error(error));
</script>
</body>
</html>