Blog侧边栏个性定位欢迎信息

侧边栏个性定位欢迎信息

参考1
参考2

本文操作步骤为 anzhiyu 主题

基于青桔气球的教程进行修改,主要改进如下

🔐 增加IP地址模糊效果
🔄 添加网络异常刷新重试
🔍 增加是否允许访问位置信息
🎨 添加加载动画和错误提示样式
💾 添加本地缓存机制,减少API调用
🏠 添加是否首页显示,开启后其它页面将不会显示这个容器

申请API密钥

首先需要在 https://api.nsmao.net/ 申请一个API密钥

操作步骤

  • 获取自己的经纬度信息
  • 创建welcome.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
window.IP_CONFIG = {
API_KEY: 'RkpZUXb9t9BSnJv1sQpbO1R48Q', // API密钥 申请地址:https://api.76.al/
BLOG_LOCATION: {
lng: 113.666, // 经度
lat: 34.666 // 纬度
},
CACHE_DURATION: 1000 * 60 * 60, // 可配置缓存时间(默认1小时)
HOME_PAGE_ONLY: true, // 是否只在首页显示 开启后其它页面将不会显示这个容器
};

const insertAnnouncementComponent = () => {
// 获取所有公告卡片
const announcementCards = document.querySelectorAll('.card-widget.card-announcement');
if (!announcementCards.length) return;

if (IP_CONFIG.HOME_PAGE_ONLY && !isHomePage()) {
announcementCards.forEach(card => card.remove());
return;
}

if (!document.querySelector('#welcome-info')) return;
fetchIpInfo();
};

const getWelcomeInfoElement = () => document.querySelector('#welcome-info');

const fetchIpData = async () => {
const response = await fetch(`https://api.nsmao.net/api/ip/query?key=${encodeURIComponent(IP_CONFIG.API_KEY)}`);
if (!response.ok) throw new Error('网络响应不正常');
return await response.json();
};

const showWelcome = ({
data,
ip
}) => {
if (!data) return showErrorMessage();

const {
lng,
lat,
country,
prov,
city
} = data;
const welcomeInfo = getWelcomeInfoElement();
if (!welcomeInfo) return;

const dist = calculateDistance(lng, lat);
const ipDisplay = formatIpDisplay(ip);
const pos = formatLocation(country, prov, city);

welcomeInfo.style.display = 'block';
welcomeInfo.style.height = 'auto';
welcomeInfo.innerHTML = generateWelcomeMessage(pos, dist, ipDisplay, country, prov, city);
};

const calculateDistance = (lng, lat) => {
const R = 6371; // 地球半径(km)
const rad = Math.PI / 180;
const dLat = (lat - IP_CONFIG.BLOG_LOCATION.lat) * rad;
const dLon = (lng - IP_CONFIG.BLOG_LOCATION.lng) * rad;
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(IP_CONFIG.BLOG_LOCATION.lat * rad) * Math.cos(lat * rad) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);

return Math.round(R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
};
const formatIpDisplay = (ip) => ip.includes(":") ? "<br>好复杂,咱看不懂~(ipv6)" : ip;
const formatLocation = (country, prov, city) => {
return country ? (country === "中国" ? `${prov} ${city}` : country) : '神秘地区';
};

const generateWelcomeMessage = (pos, dist, ipDisplay, country, prov, city) => `
欢迎来自 <b>${pos}</b> 的小友💖<br>
你当前距博主约 <b>${dist}</b> 公里!<br>
你的IP地址:<b class="ip-address">${ipDisplay}</b><br>
${getTimeGreeting()}<br>
Tip:<b>${getGreeting(country, prov, city)}🍂</b>
`;

const addStyles = () => {
const style = document.createElement('style');
style.textContent = `
#welcome-info {
user-select: none;
display: flex;
justify-content: center;
align-items: center;
height: 212px;
padding: 10px;
margin-top: 5px;
border-radius: 12px;
background-color: var(--anzhiyu-background);
outline: 1px solid var(--anzhiyu-card-border);
}
.loading-spinner {
width: 50px;
height: 50px;
border: 3px solid rgba(0, 0, 0, 0.1);
border-radius: 50%;
border-top: 3px solid var(--anzhiyu-main);
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.ip-address {
filter: blur(5px);
transition: filter 0.3s ease;
}
.ip-address:hover {
filter: blur(0);
}
.error-message {
color: #ff6565;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.error-message p,
.permission-dialog p {
margin: 0;
}
.error-icon {
font-size: 3rem;
}
#retry-button {
margin: 0 5px;
color: var(--anzhiyu-main);
transition: transform 0.3s ease;
}
#retry-button:hover {
transform: rotate(180deg);
}
.permission-dialog {
text-align: center;
}
.permission-dialog button {
margin: 10px 5px;
padding: 5px 10px;
border: none;
border-radius: 5px;
background-color: var(--anzhiyu-main);
color: white;
transition: opacity 0.3s ease;
}
.permission-dialog button:hover {
opacity: 0.8;
}
`;
document.head.appendChild(style);
};

// 位置权限相关函数
const checkLocationPermission = () => localStorage.getItem('locationPermission') === 'granted';
const saveLocationPermission = (permission) => {
localStorage.setItem('locationPermission', permission);
};
const showLocationPermissionDialog = () => {
const welcomeInfoElement = document.getElementById("welcome-info");
welcomeInfoElement.innerHTML = `
<div class="permission-dialog">
<div class="error-icon">❓</div>
<p>是否允许访问您的位置信息?</p>
<button data-action="allow">允许</button>
<button data-action="deny">拒绝</button>
</div>
`;

welcomeInfoElement.addEventListener('click', (e) => {
if (e.target.tagName === 'BUTTON') {
const action = e.target.dataset.action;
const permission = action === 'allow' ? 'granted' : 'denied';
handleLocationPermission(permission);
}
});
};
const handleLocationPermission = (permission) => {
saveLocationPermission(permission);
if (permission === 'granted') {
showLoadingSpinner();
fetchIpInfo();
} else {
showErrorMessage('您已拒绝访问位置信息');
}
};

const showLoadingSpinner = () => {
const welcomeInfoElement = document.querySelector("#welcome-info");
if (!welcomeInfoElement) return;
welcomeInfoElement.innerHTML = '<div class="loading-spinner"></div>';
};

const IP_CACHE_KEY = 'ip_info_cache';
const getIpInfoFromCache = () => {
const cached = localStorage.getItem(IP_CACHE_KEY);
if (!cached) return null;

const { data, timestamp } = JSON.parse(cached);
if (Date.now() - timestamp > IP_CONFIG.CACHE_DURATION) {
localStorage.removeItem(IP_CACHE_KEY);
return null;
}
return data;
};
const setIpInfoCache = (data) => {
localStorage.setItem(IP_CACHE_KEY, JSON.stringify({
data,
timestamp: Date.now()
}));
};

const fetchIpInfo = async () => {
if (!checkLocationPermission()) {
showLocationPermissionDialog();
return;
}

showLoadingSpinner();

const cachedData = getIpInfoFromCache();
if (cachedData) {
showWelcome(cachedData);
return;
}

try {
const data = await fetchIpData();
setIpInfoCache(data);
showWelcome(data);
} catch (error) {
console.error('获取IP信息失败:', error);
showErrorMessage();
}
};

const greetings = {
"中国": {
"北京市": "北——京——欢迎你~~~",
"天津市": "讲段相声吧",
"河北省": "山势巍巍成壁垒,天下雄关铁马金戈由此向,无限江山",
"山西省": "展开坐具长三尺,已占山河五百余",
"内蒙古自治区": "天苍苍,野茫茫,风吹草低见牛羊",
"辽宁省": "我想吃烤鸡架!",
"吉林省": "状元阁就是东北烧烤之王",
"黑龙江省": "很喜欢哈尔滨大剧院",
"上海市": "众所周知,中国只有两个城市",
"江苏省": {
"南京市": "这是我挺想去的城市啦",
"苏州市": "上有天堂,下有苏杭",
"其他": "散装是必须要散装的"
},
"浙江省": {
"杭州市": "东风渐绿西湖柳,雁已还人未南归",
"其他": "望海楼明照曙霞,护江堤白蹋晴沙"
},
"河南省": {
"郑州市": "豫州之域,天地之中",
"信阳市": "品信阳毛尖,悟人间芳华",
"南阳市": "臣本布衣,躬耕于南阳此南阳非彼南阳!",
"驻马店市": "峰峰有奇石,石石挟仙气嵖岈山的花很美哦!",
"开封市": "刚正不阿包青天",
"洛阳市": "洛阳牡丹甲天下",
"其他": "可否带我品尝河南烩面啦?"
},
"安徽省": "蚌埠住了,芜湖起飞",
"福建省": "井邑白云间,岩城远带山",
"江西省": "落霞与孤鹜齐飞,秋水共长天一色",
"山东省": "遥望齐州九点烟,一泓海水杯中泻",
"湖北省": {
"黄冈市": "红安将军县!辈出将才!",
"其他": "来碗热干面~"
},
"湖南省": "74751,长沙斯塔克",
"广东省": {
"广州市": "看小蛮腰,喝早茶了嘛~",
"深圳市": "今天你逛商场了嘛~",
"阳江市": "阳春合水!博主家乡~ 欢迎来玩~",
"其他": "来两斤福建人~"
},
"广西壮族自治区": "桂林山水甲天下",
"海南省": "朝观日出逐白浪,夕看云起收霞光",
"四川省": "康康川妹子",
"贵州省": "茅台,学生,再塞200",
"云南省": "玉龙飞舞云缠绕,万仞冰川直耸天",
"西藏自治区": "躺在茫茫草原上,仰望蓝天",
"陕西省": "来份臊子面加馍",
"甘肃省": "羌笛何须怨杨柳,春风不度玉门关",
"青海省": "牛肉干和老酸奶都好好吃",
"宁夏回族自治区": "大漠孤烟直,长河落日圆",
"新疆维吾尔自治区": "驼铃古道丝绸路,胡马犹闻唐汉风",
"台湾省": "我在这头,大陆在那头",
"香港特别行政区": "永定贼有残留地鬼嚎,迎击光非岁玉",
"澳门特别行政区": "性感荷官,在线发牌",
"其他": "带我去你的城市逛逛吧!"
},
"美国": "Let us live in peace!",
"日本": "よろしく、一緒に桜を見ませんか",
"俄罗斯": "干了这瓶伏特加!",
"法国": "C'est La Vie",
"德国": "Die Zeit verging im Fluge.",
"澳大利亚": "一起去大堡礁吧!",
"加拿大": "拾起一片枫叶赠予你",
"其他": "带我去你的国家逛逛吧"
};

const getGreeting = (country, province, city) => {
const countryGreeting = greetings[country] || greetings["其他"];
if (typeof countryGreeting === 'string') {
return countryGreeting;
}
const provinceGreeting = countryGreeting[province] || countryGreeting["其他"];
if (typeof provinceGreeting === 'string') {
return provinceGreeting;
}
return provinceGreeting[city] || provinceGreeting["其他"] || countryGreeting["其他"];
};
const getTimeGreeting = () => {
const hour = new Date().getHours();
if (hour < 11) return "早上好🌤️ ,一日之计在于晨";
if (hour < 13) return "中午好☀️ ,记得午休喔~";
if (hour < 17) return "下午好🕞 ,饮茶先啦!";
if (hour < 19) return "即将下班🚶‍♂️,记得按时吃饭~";
return "晚上好🌙 ,夜生活嗨起来!";
};

const showErrorMessage = (message = '抱歉,无法获取信息') => {
const welcomeInfoElement = document.getElementById("welcome-info");
welcomeInfoElement.innerHTML = `
<div class="error-message">
<div class="error-icon">😕</div>
<p>${message}</p>
<p>请<i id="retry-button" class="fa-solid fa-arrows-rotate"></i>重试或检查网络连接</p>
</div>
`;

document.getElementById('retry-button').addEventListener('click', fetchIpInfo);
};

const isHomePage = () => {
return window.location.pathname === '/' || window.location.pathname === '/index.html';
};

// 初始化
document.addEventListener('DOMContentLoaded', () => {
addStyles();
insertAnnouncementComponent();
document.addEventListener('pjax:complete', insertAnnouncementComponent);
});
  • _config.anzhiyu.yml主题配置文件下inject配置项中的bottom引入welcome.js

  • 确认_config.anzhiyu.yml主题配置文件下aside配置项中的card_announcement是否开启,配置参考

1
2
3
4
5
6
7
card_announcement:
enable: true # 必须开启
content: <span>👋🏻 Hi,我是xiaoze,欢迎你!</span><br>
<span>❓ 如有问题欢迎评论区交流!</span><br>
<span>😫 页面异常?尝试<kbd>Ctrl</kbd>+<kbd>F5</kbd></span><br>
<span>📧 如需联系我:<a href="mailto:xxx@qq.com"><b>发送邮件🚀</b></a></span>
<div id="welcome-info"></div> # 必须项

注意事项

  1. 确保API密钥正确填写,正确设置白名单
  2. 经纬度没必要填写详细,为了自身隐私安全
  3. 选择器可以根据你的主题实际情况修改

常见问题

Q: 为什么显示”无法获取信息”?
A: 可能是API密钥错误或者网络问题,检查配置并重试。

Q: IP地址显示不准确?
A: 正常现象,当前接口的IP库不够精准,后续会优化

操作步骤(1.0旧版)

  • 打开腾讯位置服务,登录你的账号
  • 点击创建应用,填一下名称和类型,随意即可
  • 点击添加Key,key名称随意,勾选WebServiceAPI,此处白名单可以自选
    例如我选的是域名白名单,注意此处本地部署localhost:4000是有效的,需带上端口号。
    部署至公网填写域名即可,不需要端口号。
  • 记录下得到的Key
  • _config.anzhiyu.yml主题配置文件下inject配置项中的bottom引入jquery.min.js
1
2
3
4
inject:

bottom:
- <script type="text/javascript" src="https://unpkg.zhimg.com/jquery@latest/dist/jquery.min.js"></script> # jquery.min.js
  • 在hexo目录下新建source/js/welcome.js文件,新增以下内容(若没有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
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
237
238
239
240
241
242
243
244
245
246
247
248
//get请求
$.ajax({
type: 'get',
url: 'https://apis.map.qq.com/ws/location/v1/ip',
data: {
key: '你刚刚获取到的Key',
output: 'jsonp',
},
dataType: 'jsonp',
success: function (res) {
ipLocation = res;
}
})
function getDistance(e1, n1, e2, n2) {
const R = 6371
const { sin, cos, asin, PI, hypot } = Math
let getPoint = (e, n) => {
e *= PI / 180
n *= PI / 180
return { x: cos(n) * cos(e), y: cos(n) * sin(e), z: sin(n) }
}

let a = getPoint(e1, n1)
let b = getPoint(e2, n2)
let c = hypot(a.x - b.x, a.y - b.y, a.z - b.z)
let r = asin(c / 2) * 2 * R
return Math.round(r);
}

function showWelcome() {

let dist = getDistance(114.21416, 22.576264, ipLocation.result.location.lng, ipLocation.result.location.lat); //这里记得换成自己的经纬度
let pos = ipLocation.result.ad_info.nation;
let ip;
let posdesc;
//根据国家、省份、城市信息自定义欢迎语
switch (ipLocation.result.ad_info.nation) {
case "日本":
posdesc = "よろしく,一起去看樱花吗";
break;
case "美国":
posdesc = "Let us live in peace!";
break;
case "英国":
posdesc = "想同你一起夜乘伦敦眼";
break;
case "俄罗斯":
posdesc = "干了这瓶伏特加!";
break;
case "法国":
posdesc = "C'est La Vie";
break;
case "德国":
posdesc = "Die Zeit verging im Fluge.";
break;
case "澳大利亚":
posdesc = "一起去大堡礁吧!";
break;
case "加拿大":
posdesc = "拾起一片枫叶赠予你";
break;
case "中国":
pos = ipLocation.result.ad_info.province + " " + ipLocation.result.ad_info.city + " " + ipLocation.result.ad_info.district;
ip = ipLocation.result.ip;
switch (ipLocation.result.ad_info.province) {
case "北京市":
posdesc = "北——京——欢迎你~~~";
break;
case "天津市":
posdesc = "讲段相声吧";
break;
case "河北省":
posdesc = "山势巍巍成壁垒,天下雄关铁马金戈由此向,无限江山";
break;
case "山西省":
posdesc = "展开坐具长三尺,已占山河五百余";
break;
case "内蒙古自治区":
posdesc = "天苍苍,野茫茫,风吹草低见牛羊";
break;
case "辽宁省":
posdesc = "我想吃烤鸡架!";
break;
case "吉林省":
posdesc = "状元阁就是东北烧烤之王";
break;
case "黑龙江省":
posdesc = "很喜欢哈尔滨大剧院";
break;
case "上海市":
posdesc = "众所周知,中国只有两个城市";
break;
case "江苏省":
switch (ipLocation.result.ad_info.city) {
case "南京市":
posdesc = "这是我挺想去的城市啦";
break;
case "苏州市":
posdesc = "上有天堂,下有苏杭";
break;
default:
posdesc = "散装是必须要散装的";
break;
}
break;
case "浙江省":
posdesc = "东风渐绿西湖柳,雁已还人未南归";
break;
case "河南省":
switch (ipLocation.result.ad_info.city) {
case "郑州市":
posdesc = "豫州之域,天地之中";
break;
case "南阳市":
posdesc = "臣本布衣,躬耕于南阳此南阳非彼南阳!";
break;
case "驻马店市":
posdesc = "峰峰有奇石,石石挟仙气嵖岈山的花很美哦!";
break;
case "开封市":
posdesc = "刚正不阿包青天";
break;
case "洛阳市":
posdesc = "洛阳牡丹甲天下";
break;
default:
posdesc = "可否带我品尝河南烩面啦?";
break;
}
break;
case "安徽省":
posdesc = "蚌埠住了,芜湖起飞";
break;
case "福建省":
posdesc = "井邑白云间,岩城远带山";
break;
case "江西省":
posdesc = "落霞与孤鹜齐飞,秋水共长天一色";
break;
case "山东省":
posdesc = "遥望齐州九点烟,一泓海水杯中泻";
break;
case "湖北省":
switch (ipLocation.result.ad_info.city) {
case "黄冈市":
posdesc = "红安将军县!辈出将才!";
break;
default:
posdesc = "来碗热干面~";
break;
}
break;
case "湖南省":
posdesc = "74751,长沙斯塔克";
break;
case "广东省":
switch (ipLocation.result.ad_info.city) {
case "广州市":
posdesc = "看小蛮腰,喝早茶了嘛~";
break;
case "深圳市":
posdesc = "今天你逛商场了嘛~";
break;
case "阳江市":
posdesc = "阳春合水!博主家乡~ 欢迎来玩~";
break;
default:
posdesc = "来两斤福建人~";
break;
}
break;
case "广西壮族自治区":
posdesc = "桂林山水甲天下";
break;
case "海南省":
posdesc = "朝观日出逐白浪,夕看云起收霞光";
break;
case "四川省":
posdesc = "康康川妹子";
break;
case "贵州省":
posdesc = "茅台,学生,再塞200";
break;
case "云南省":
posdesc = "玉龙飞舞云缠绕,万仞冰川直耸天";
break;
case "西藏自治区":
posdesc = "躺在茫茫草原上,仰望蓝天";
break;
case "陕西省":
posdesc = "来份臊子面加馍";
break;
case "甘肃省":
posdesc = "羌笛何须怨杨柳,春风不度玉门关";
break;
case "青海省":
posdesc = "牛肉干和老酸奶都好好吃";
break;
case "宁夏回族自治区":
posdesc = "大漠孤烟直,长河落日圆";
break;
case "新疆维吾尔自治区":
posdesc = "驼铃古道丝绸路,胡马犹闻唐汉风";
break;
case "台湾省":
posdesc = "我在这头,大陆在那头";
break;
case "香港特别行政区":
posdesc = "永定贼有残留地鬼嚎,迎击光非岁玉";
break;
case "澳门特别行政区":
posdesc = "性感荷官,在线发牌";
break;
default:
posdesc = "带我去你的城市逛逛吧!";
break;
}
break;
default:
posdesc = "带我去你的国家逛逛吧";
break;
}

//根据本地时间切换欢迎语
let timeChange;
let date = new Date();
if (date.getHours() >= 5 && date.getHours() < 11) timeChange = "<span>🌤️ 早上好,一日之计在于晨</span>";
else if (date.getHours() >= 11 && date.getHours() < 13) timeChange = "<span>☀️ 中午好,记得午休喔~</span>";
else if (date.getHours() >= 13 && date.getHours() < 17) timeChange = "<span>🕞 下午好,饮茶先啦!</span>";
else if (date.getHours() >= 17 && date.getHours() < 19) timeChange = "<span>🚶‍♂️ 即将下班,记得按时吃饭~</span>";
else if (date.getHours() >= 19 && date.getHours() < 24) timeChange = "<span>🌙 晚上好,夜生活嗨起来!</span>";
else timeChange = "夜深了,早点休息,少熬夜";

// 新增ipv6显示为指定内容
if (ip.includes(":")) {
ip = "<br>好复杂,咱看不懂~(ipv6)";
}
try {
//自定义文本和需要放的位置
document.getElementById("welcome-info").innerHTML =
`欢迎来自 <b><span style="color: var(--kouseki-ip-color);font-size: var(--kouseki-gl-size)">${pos}</span></b> 的小友💖<br>${posdesc}🍂<br>当前位置距博主约 <b><span style="color: var(--kouseki-ip-color)">${dist}</span></b> 公里!<br>您的IP地址为:<b><span>${ip}</span></b><br>${timeChange} <br>`;
} catch (err) {
console.log("Pjax无法获取元素")
}
}
window.onload = showWelcome;
// 如果使用了pjax在加上下面这行代码
document.addEventListener('pjax:complete', showWelcome);
  • 在第6行及第32行分别填入自己的Key和经纬度
  • _config.anzhiyu.yml主题配置文件下inject配置项中的bottom引入welcome.js
1
2
3
4
inject:

bottom:
- <script defer="true" src="/js/welcome.js"></script> # 个性定位欢迎信息
  • 在hexo目录下新建source/welcome.css文件,新增以下内容
1
2
3
4
5
6
7
#welcome-info {
overflow: hidden;
border-radius: 14px;
--kouseki-welcome-color: #49B1F5;
--kouseki-ip-color: #49B1F5;
--kouseki-gl-size: 16px!important;
}
  • _config.anzhiyu.yml主题配置文件下inject配置项中的head引入welcome.css
1
2
3
4
inject:

head:
- <link rel="stylesheet" href="/css/welcome.css?1">
  • 至此已经整体配置完毕,可以将相应的div容器放在需要的位置~,例如放到anzhiyu主题的公告栏位置

_config.anzhiyu.yml主题配置文件下aside配置项中添加以下内容

1
2
3
card_announcement:
enable: true
content: <div id="welcome-info"></div>

到此就大功告成啦,hexo三连看看效果吧!