以下是“傻瓜式”插件脚本,只需要将它插入到任意页面的底部(如 </body> 之前),即可自动获取 IP 定位并展示当前天气的动态效果。

✅ 效果:

天气类型 动画效果 音效文件
☀️ 晴天(sun) 阳光光晕、日出光线 birdsong.mp3(鸟鸣)
🌧️ 雨天(rain) 雨滴动态、地面渐变 rain.mp3(雨声)
❄️ 雪天(snow) 飘雪效果 snow.mp3(风中轻雪声)
🌩️ 雷暴(storm) 雨+闪电+白屏 thunder.mp3(雷鸣)
☁️ 阴天/多云(cloud) 飘云、偶有风 wind.mp3(风声)
🍂 大风(wind) 落叶飘零 wind2.mp3(呼啸大风)

✅ 特性:

功能 描述
✅ 自动定位天气 使用 wttr.in 自动检测本地天气
✅ 粒子动画特效 根据天气动态绘制 Canvas(雨、雪、云、阳光、雷电、落叶)
✅ 天气音效播放 每种天气对应音效自动播放 + 可手动控制
✅ 控制按钮 页面右上角按钮【🔊/🔇】可切换静音
✅ 全屏透明 Canvas fixed + pointer-events: none完全不影响页面交互和滚动

✅ 插件说明:

  • 所有音效通过 HTML5 Audio 播放,默认 自动播放 + 循环(无用户交互限制时自动播放)。
  • canvas 渲染、fixed 透明、pointer-events: none,不干扰网页原本功能。
  • 音效来源为公开免费资源或你可替换为自有 CDN 地址。

代码

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
<script>
(async () => {
// 初始化 Canvas
const canvas = document.createElement('canvas');
Object.assign(canvas.style, {
position: 'fixed',
top: 0,
left: 0,
width: '100vw',
height: '100vh',
zIndex: 9998,
pointerEvents: 'none',
});
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});

// 添加控制按钮
const btn = document.createElement('div');
btn.textContent = '🔊';
Object.assign(btn.style, {
position: 'fixed',
top: '10px',
right: '10px',
zIndex: 9999,
cursor: 'pointer',
background: 'rgba(0,0,0,0.4)',
padding: '6px 10px',
borderRadius: '8px',
fontSize: '20px',
color: '#fff',
userSelect: 'none'
});
document.body.appendChild(btn);

// 音效资源映射
const soundMap = {
sun: 'https://lhttp.qtfm.cn/live/4915/64k.mp3',
rain: 'https://lhttp.qtfm.cn/live/5022405/64k.mp3',
snow: 'https://lhttp.qtfm.cn/live/4917/64k.mp3',
storm: 'https://lhttp.qtfm.cn/live/4915/64k.mp3',
cloud: 'https://lhttp.qtfm.cn/live/5022405/64k.mp3',
wind: 'https://lhttp.qtfm.cn/live/5022405/64k.mp3',
};

let currentAudio = null;
let muted = false;
btn.onclick = () => {
muted = !muted;
btn.textContent = muted ? '🔇' : '🔊';
if (currentAudio) currentAudio.muted = muted;
};

const playSound = (type) => {
if (currentAudio) currentAudio.pause();
const src = soundMap[type];
if (src) {
currentAudio = new Audio(src);
currentAudio.loop = true;
currentAudio.volume = 0.5;
currentAudio.muted = muted;
currentAudio.play().catch(() => {
console.warn('浏览器可能阻止了自动播放。请用户交互后允许。');
});
}
};

// 获取天气
const weatherData = await fetch('https://wttr.in/?format=j1&lang=zh-cn').then(r => r.json()).catch(() => null);
const desc = weatherData?.current_condition?.[0]?.weatherDesc?.[0]?.value || '';
/*{ const type = (() => {
const t = desc.toLowerCase();
if (t.includes('雪')) return 'snow';
if (t.includes('雷')) return 'storm';
if (t.includes('雨')) return 'rain';
if (t.includes('云')) return 'cloud';
if (t.includes('风')) return 'wind';
if (t.includes('晴')) return 'sun';
return 'sun';
})(); */

const type = (() => {
const t = desc.toLowerCase();
if (t.includes('snow')) return 'snow';
if (t.includes('thunder')) return 'storm';
if (t.includes('rain') || t.includes('drizzle')) return 'rain';
if (t.includes('cloud')) return 'cloud';
if (t.includes('wind')) return 'wind';
if (t.includes('sunny') || t.includes('clear')) return 'sun';
return 'sun';
})();
playSound(type);

// 粒子数组
const particles = [];

// 初始化粒子
const initParticles = () => {
const w = canvas.width;
const h = canvas.height;
particles.length = 0;
if (type === 'rain' || type === 'storm') {
for (let i = 0; i < 200; i++) {
particles.push({
x: Math.random() * w,
y: Math.random() * h,
l: Math.random() * 20 + 10,
speed: Math.random() * 4 + 4,
type: 'rain',
});
}
} else if (type === 'snow') {
for (let i = 0; i < 100; i++) {
particles.push({
x: Math.random() * w,
y: Math.random() * h,
r: Math.random() * 3 + 1,
speed: Math.random() + 0.5,
drift: Math.random() * 0.5 - 0.25,
type: 'snow',
});
}
} else if (type === 'cloud') {
for (let i = 0; i < 6; i++) {
particles.push({
x: Math.random() * w,
y: Math.random() * 150,
{/* w: 120 + Math.random() * 100,
h: 60 + Math.random() * 20, */}
w: 50 + Math.random() * 70,
h: 20 + Math.random() * 30,
speed: Math.random() * 0.2 + 0.1,
type: 'cloud',
});
}
} else if (type === 'wind') {
for (let i = 0; i < 50; i++) {
particles.push({
x: Math.random() * w,
y: Math.random() * h,
angle: Math.random() * 360,
r: Math.random() * 8 + 4,
speed: 1 + Math.random(),
swing: Math.random() * 1.5,
type: 'leaf',
});
}
} else if (type === 'sun') {
// sun + 落叶
for (let i = 0; i < 30; i++) {
particles.push({
x: Math.random() * w,
y: Math.random() * h,
angle: Math.random() * 360,
r: Math.random() * 8 + 4,
speed: 1 + Math.random(),
swing: Math.random() * 1.5,
type: 'leaf',
});
}
}
};

initParticles();

let flash = 0;

// 动画循环
function animate() {
const w = canvas.width;
const h = canvas.height;
ctx.clearRect(0, 0, w, h);

// 渐变背景层
const bg = ctx.createLinearGradient(0, 0, 0, h);
bg.addColorStop(0, 'rgba(255,255,255,0.05)');
bg.addColorStop(1, 'rgba(0,0,0,0.05)');
ctx.fillStyle = bg;
ctx.fillRect(0, 0, w, h);

// 太阳效果
{/* if (type === 'sun') {
const cx = w - 120;
const cy = 100;
const gradient = ctx.createRadialGradient(cx, cy, 0, cx, cy, 100);
gradient.addColorStop(0, 'rgba(255,255,0,0.6)');
gradient.addColorStop(1, 'rgba(255,255,0,0)');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(cx, cy, 100, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = 'rgba(255,204,0,0.8)';
ctx.beginPath();
ctx.arc(cx, cy, 40, 0, 2 * Math.PI);
ctx.fill();
} */}

// 粒子绘制
for (let p of particles) {
if (p.type === 'rain') {
ctx.strokeStyle = 'rgba(173,216,230,0.5)';
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(p.x, p.y + p.l);
ctx.stroke();
p.y += p.speed;
if (p.y > h) {
p.y = -20;
p.x = Math.random() * w;
}
} else if (p.type === 'snow') {
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(p.x, p.y, p.r, 0, 2 * Math.PI);
ctx.fill();
p.y += p.speed;
p.x += p.drift;
if (p.y > h) {
p.y = -10;
p.x = Math.random() * w;
}
} else if (p.type === 'cloud') {
// 多彩渐变云朵
const grad = ctx.createRadialGradient(p.x, p.y, 10, p.x, p.y, p.w);
const colors = [
'rgba(255,255,255,0.7)',
'rgba(173,216,230,0.5)',
'rgba(255,182,193,0.4)',
'rgba(255,255,224,0.3)',
'rgba(144,238,144,0.3)',
];
grad.addColorStop(0, colors[Math.floor(Math.random() * colors.length)]);
grad.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = grad;
//ctx.fillStyle = 'rgba(255,255,255,0.6)';
ctx.beginPath();
ctx.ellipse(p.x, p.y, p.w, p.h, 0, 0, 2 * Math.PI);
ctx.fill();
p.x += p.speed;
if (p.x > w + p.w) p.x = -p.w;
} else if (p.type === 'leaf') {
ctx.save();
ctx.translate(p.x, p.y);
ctx.rotate(p.angle * Math.PI / 180);
ctx.fillStyle = 'rgba(255,165,0,0.7)';
ctx.beginPath();
ctx.ellipse(0, 0, p.r, p.r / 2, 0, 0, 2 * Math.PI);
ctx.fill();
ctx.restore();
p.y += p.speed;
p.x += Math.sin(p.angle * Math.PI / 180) * p.swing;
p.angle += 2;
if (p.y > h) p.y = -10;
}
}

// 闪电效果
if (type === 'storm') {
if (Math.random() < 0.005) flash = 1.0;
if (flash > 0) {
ctx.fillStyle = `rgba(255,255,255,${flash})`;
ctx.fillRect(0, 0, w, h);
flash -= 0.05;
}
}

requestAnimationFrame(animate);
}

animate();
})();
</script>