冬天的时候给博客加了下雪特效,在这一篇博客的文末提了一嘴 –感冒了。
当时当时忘记加自动关闭的功能了。
今天抽五分钟补一下,需要脚本的自取,附在本文末了。
说起为啥想起来要补关闭特效的功能,还是在「灰常记忆」的博客中,看到了「网友小宋」的留言才记起。

看了下最近的天气,反复无常,又在下雨。
上海的气温一如既往的不稳定,谨防感冒,注意添衣。

后记
我又修改了脚本,并且添加了樱花🌸效果,主要的修改为:
- 去除定时关闭,以后尝试不同的季节用不同的树叶飘落效果
- 增加樱花图片/将雪花地址更新为樱花图片地址
- 后面尝试添加天气效果,先待定
| 12
 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
 
 | (function() {
 const snowflakeCount = 10;
 const minSize = 15;
 const maxSize = 30;
 const windEffect = 40;
 const snowflakeImage = 'https://vrast.cn/img/snow.png';
 
 
 function createSnowflake() {
 const snowflake = document.createElement('div');
 snowflake.className = 'snowflake';
 
 
 const size = Math.random() * (maxSize - minSize) + minSize;
 snowflake.style.width = `${size}px`;
 snowflake.style.height = `${size}px`;
 
 
 const leftPosition = Math.random() * 100;
 const animationDuration = Math.random() * 3 + 3;
 const delayTime = Math.random() * 5 + 's';
 
 snowflake.style.left = `${leftPosition}vw`;
 snowflake.style.position = 'fixed';
 snowflake.style.top = `${-size}px`;
 snowflake.style.pointerEvents = 'none';
 snowflake.style.userSelect = 'none';
 snowflake.style.zIndex = 9999;
 
 
 snowflake.style.backgroundImage = `url(${snowflakeImage})`;
 snowflake.style.backgroundSize = 'cover';
 
 
 snowflake.style.animation = `fall ${animationDuration}s linear infinite`;
 snowflake.style.animationDelay = delayTime;
 
 
 const rotateAngle = Math.random() * 360;
 const translateX = (Math.random() - 0.5) * windEffect;
 snowflake.style.transform = `rotate(${rotateAngle}deg) translateX(${translateX}vw)`;
 
 
 document.body.appendChild(snowflake);
 }
 
 
 function initCSS() {
 const style = document.createElement('style');
 style.innerHTML = `
 /* 雪花的样式 - 使用图片 */
 .snowflake {
 position: absolute;
 background-color: transparent;
 opacity: 1;
 transform-origin: center;
 pointer-events: none;
 user-select: none;
 }
 
 /* 雪花下落动画 */
 @keyframes fall {
 to {
 transform: translateY(100vh); /* 让雪花从顶部飘落到屏幕底部 */
 }
 }
 `;
 document.head.appendChild(style);
 }
 
 
 function initSnowfall() {
 for (let i = 0; i < snowflakeCount; i++) {
 createSnowflake();
 }
 }
 
 
 function init() {
 initCSS();
 initSnowfall();
 }
 
 
 const currentDate = new Date();
 
 const currentMonth = currentDate.getMonth();
 
 
 function isWinter(month) {
 
 return month === 11 || month === 0 || month === 1;
 }
 
 
 if (isWinter(currentMonth)) {
 
 init();
 }
 })();
 
 
 | 
本文标题:给雪花特效添加自动开启与关闭
文章作者:Keyle
发布时间:2025-03-11
最后更新:2025-03-13
原始链接:https://vrast.cn/posts/28284/
版权声明:©Keyle's Blog. 本站采用署名-非商业性使用-相同方式共享 4.0 国际进行许可