前言

金价数据卡片,使用透明磨砂框样式,显示实时金价信息。

api接口地址: 实时查询黄金价格

效果图

效果

配置方法

创建widget.yml文件

如果没有这个目录需要创建,source/_data/widget.yml

1
2
3
4
5
6
7
# 这里填top表示所有地方都显示,bottom表示只在非文章页面显示,如主页等等
bottom:
- class_name:
id_name: gold_price
name: 今日金价
icon: fa-solid fa-coins
html: <link rel="stylesheet" href="/css/gold_price.css"><div id="gold_price_content"></div><script src="/js/gold_price.js"></script>

创建gold_price.css

创建themes/butterfly/source/css/gold_price.css

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
/* 金价数据卡片样式 - 透明磨砂框 */
#gold_price.card-widget {
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border-radius: 12px;
color: white;
padding: 30px 20px 30px;
}

#gold_price_content {
width: 100%;
height: 280px;
font-size: 90%;
overflow-y: auto;
-ms-overflow-style: none;
scrollbar-width: none;
display: flex;
flex-direction: column;
gap: 12px;
}

/* 自定义滚动条 */
#gold_price_content::-webkit-scrollbar {
width: 4px;
}
#gold_price_content::-webkit-scrollbar-track {
background: rgba(0, 0, 0, 0.3);
border-radius: 2px;
}
#gold_price_content::-webkit-scrollbar-thumb {
background: rgba(255, 215, 0, 0.5);
border-radius: 2px;
}

/* 日期显示 */
.gold-date {
text-align: center;
font-size: 12px;
color: rgba(255, 215, 0, 0.8);
margin-bottom: 8px;
font-weight: 500;
}

/* 金价卡片 - 透明磨砂效果 */
.gold-card {
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
border-radius: 8px;
padding: 12px;
border: none;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

/* 品种名称 */
.gold-name {
font-size: 14px;
font-weight: 600;
color: rgba(255, 215, 0, 0.9);
margin-bottom: 8px;
padding-bottom: 6px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

/* 数据项 */
.gold-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 4px 0;
font-size: 13px;
color: rgba(255, 255, 255, 0.8);
}

/* 价格值 */
.gold-value {
color: rgba(255, 215, 0, 0.9);
font-weight: 600;
font-family: 'Arial', sans-serif;
}

/* 空数据提示 */
.gold-empty {
text-align: center;
padding: 30px 0;
color: rgba(255, 255, 255, 0.5);
font-size: 14px;
}

/* 适配移动端 */
@media (max-width: 768px) {
#newsContent {
gap: 10px;
}
.gold-card {
padding: 10px;
}
.gold-item {
font-size: 12px;
}
}

创建gold_price.js文件

创建themes/butterfly/source/js/gold_price.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
// 金价数据卡片逻辑 - 透明磨砂框显示
(function() {
const API_URL = 'https://tmini.net/api/gold-price?type=json';
const CACHE_DURATION = 5 * 60 * 1000;
const CACHE_KEY = 'gold_price_cache';

function goldPrice() {
let html = '';
let data = JSON.parse(localStorage.getItem(CACHE_KEY));
let nowTime = Date.now();

if (data == null || nowTime - data.timestamp > CACHE_DURATION) {
getData();
return;
} else {
renderGoldPrice(data.data);
return;
}
}

function renderGoldPrice(data) {
let html = '';
if (!data || !data.metals || data.metals.length === 0) {
html = '<div class="gold-empty">暂无金价数据~</div>';
} else {
const date = data.date || '未知时间';
html += `<div class="gold-date">${date}</div>`;

data.metals.forEach((metal, index) => {
html += `
<div class="gold-card">
<div class="gold-name">${metal.name || '未知品种'}</div>
<div class="gold-item">当前卖出价:<span class="gold-value">${metal.sell_price || '-'}</span></div>
<div class="gold-item">今日开盘价:<span class="gold-value">${metal.today_price || '-'}</span></div>
<div class="gold-item">今日最高价:<span class="gold-value">${metal.high_price || '-'}</span></div>
<div class="gold-item">今日最低价:<span class="gold-value">${metal.low_price || '-'}</span></div>
</div>
`;
});
}
document.getElementById('gold_price_content').innerHTML = html;
}

function getData() {
fetch(API_URL, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) throw new Error(`状态码: ${response.status}`);
return response.json();
})
.then(data => {
const cacheData = {
timestamp: Date.now(),
data: data
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cacheData));
renderGoldPrice(data);
})
.catch(error => {
console.error('获取金价数据失败:', error);
document.getElementById('gold_price_content').innerHTML = '<div class="gold-empty">暂无金价数据~</div>';
});
}

try {
if (document.getElementById('gold_price').clientWidth) goldPrice();
} catch (error) {
goldPrice();
}
})();

加入卡片

1
2
3
4
5
aside:
..........
card_webinfo:
enable: true
post_count: true

调整侧边栏顺序

调整sort_order大小即可,数字越大越前,要把公告后的卡片都加上数字(有的资料说是越小越前,但是我测试不是,反正能用就行)