下雪方向受鼠标方向影响
snowStorm.js

/** @license* DHTML Snowstorm! JavaScript-based Snow for web pages* --------------------------------------------------------* Version 1.43.20111201 (Previous rev: 1.42.20111120)* Copyright (c) 2007, Scott Schiller. All rights reserved.* Code provided under the BSD License:* http://schillmania.com/projects/snowstorm/license.txt*//*global window, document, navigator, clearInterval, setInterval */
/*jslint white: false, onevar: true, plusplus: false, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */var snowStorm = (function(window, document) {// --- common properties ---this.autoStart = true;          // Whether the snow should start automatically or not.this.flakesMax = 60;           // Limit total amount of snow made (falling + sticking)this.flakesMaxActive = 60;      // Limit amount of snow falling at once (less = lower CPU use)this.animationInterval = 40;    // Theoretical "miliseconds per frame" measurement. 20 = fast + smooth, but high CPU use. 50 = more conservative, but slowerthis.excludeMobile = true;      // Snow is likely to be bad news for mobile phones' CPUs (and batteries.) By default, be nice.this.flakeBottom = null;        // Integer for Y axis snow limit, 0 or null for "full-screen" snow effectthis.followMouse = true;        // Snow movement can respond to the user's mousethis.snowColor = '#fff';        // Don't eat (or use?) yellow snow.this.snowCharacter = '&bull;';  // &bull; = bullet, &middot; is square on some systems etc.this.snowStick = false;          // Whether or not snow should "stick" at the bottom. When off, will never collect.this.targetElement = null;      // element which snow will be appended to (null = document.body) - can be an element ID eg. 'myDiv', or a DOM node referencethis.useMeltEffect = true;      // When recycling fallen snow (or rarely, when falling), have it "melt" and fade out if browser supports itthis.useTwinkleEffect = false;  // Allow snow to randomly "flicker" in and out of view while fallingthis.usePositionFixed = false;  // true = snow does not shift vertically when scrolling. May increase CPU load, disabled by default - if enabled, used only where supported// --- less-used bits ---this.freezeOnBlur = true;       // Only snow when the window is in focus (foreground.) Saves CPU.this.flakeLeftOffset = 0;       // Left margin/gutter space on edge of container (eg. browser window.) Bump up these values if seeing horizontal scrollbars.this.flakeRightOffset = 0;      // Right margin/gutter space on edge of containerthis.flakeWidth = 5;            // Max pixel width reserved for snow elementthis.flakeHeight = 5;           // Max pixel height reserved for snow elementthis.vMaxX = 2.5;                 // Maximum X velocity range for snowthis.vMaxY = 2.5;                 // Maximum Y velocity range for snowthis.zIndex = 100000;                // CSS stacking order applied to each snowflake// --- End of user section ---var s = this, storm = this, i,// UA sniffing and backCompat rendering mode checks for fixed position, etc.isIE = navigator.userAgent.match(/msie/i),isIE6 = navigator.userAgent.match(/msie 6/i),isWin98 = navigator.appVersion.match(/windows 98/i),isMobile = navigator.userAgent.match(/mobile|opera m(ob|in)/i),isBackCompatIE = (isIE && document.compatMode === 'BackCompat'),noFixed = (isMobile || isBackCompatIE || isIE6),screenX = null, screenX2 = null, screenY = null, scrollY = null, vRndX = null, vRndY = null,windOffset = 1,windMultiplier = 2,flakeTypes = 6,fixedForEverything = false,opacitySupported = (function(){try {document.createElement('div').style.opacity = '0.5';} catch(e) {return false;}return true;}()),didInit = false,docFrag = document.createDocumentFragment();this.timers = [];this.flakes = [];this.disabled = false;this.active = false;this.meltFrameCount = 20;this.meltFrames = [];this.events = (function() {var old = (!window.addEventListener && window.attachEvent), slice = Array.prototype.slice,evt = {add: (old?'attachEvent':'addEventListener'),remove: (old?'detachEvent':'removeEventListener')};function getArgs(oArgs) {var args = slice.call(oArgs), len = args.length;if (old) {args[1] = 'on' + args[1]; // prefixif (len > 3) {args.pop(); // no capture}} else if (len === 3) {args.push(false);}return args;}function apply(args, sType) {var element = args.shift(),method = [evt[sType]];if (old) {element[method](args[0], args[1]);} else {element[method].apply(element, args);}}function addEvent() {apply(getArgs(arguments), 'add');}function removeEvent() {apply(getArgs(arguments), 'remove');}return {add: addEvent,remove: removeEvent};}());function rnd(n,min) {if (isNaN(min)) {min = 0;}return (Math.random()*n)+min;}function plusMinus(n) {return (parseInt(rnd(2),10)===1?n*-1:n);}this.randomizeWind = function() {var i;vRndX = plusMinus(rnd(s.vMaxX,0.2));vRndY = rnd(s.vMaxY,0.2);if (this.flakes) {for (i=0; i<this.flakes.length; i++) {if (this.flakes[i].active) {this.flakes[i].setVelocities();}}}};this.scrollHandler = function() {var i;// "attach" snowflakes to bottom of window if no absolute bottom value was givenscrollY = (s.flakeBottom?0:parseInt(window.scrollY||document.documentElement.scrollTop||document.body.scrollTop,10));if (isNaN(scrollY)) {scrollY = 0; // Netscape 6 scroll fix}if (!fixedForEverything && !s.flakeBottom && s.flakes) {for (i=s.flakes.length; i--;) {if (s.flakes[i].active === 0) {s.flakes[i].stick();}}}};this.resizeHandler = function() {if (window.innerWidth || window.innerHeight) {screenX = window.innerWidth-16-s.flakeRightOffset;screenY = (s.flakeBottom?s.flakeBottom:window.innerHeight);} else {screenX = (document.documentElement.clientWidth||document.body.clientWidth||document.body.scrollWidth)-(!isIE?8:0)-s.flakeRightOffset;screenY = s.flakeBottom?s.flakeBottom:(document.documentElement.clientHeight||document.body.clientHeight||document.body.scrollHeight);}screenX2 = parseInt(screenX/2,10);};this.resizeHandlerAlt = function() {screenX = s.targetElement.offsetLeft+s.targetElement.offsetWidth-s.flakeRightOffset;screenY = s.flakeBottom?s.flakeBottom:s.targetElement.offsetTop+s.targetElement.offsetHeight;screenX2 = parseInt(screenX/2,10);};this.freeze = function() {// pause animationvar i;if (!s.disabled) {s.disabled = 1;} else {return false;}for (i=s.timers.length; i--;) {clearInterval(s.timers[i]);}};this.resume = function() {if (s.disabled) {s.disabled = 0;} else {return false;}s.timerInit();};this.toggleSnow = function() {if (!s.flakes.length) {// first runs.start();} else {s.active = !s.active;if (s.active) {s.show();s.resume();} else {s.stop();s.freeze();}}};this.stop = function() {var i;this.freeze();for (i=this.flakes.length; i--;) {this.flakes[i].o.style.display = 'none';}s.events.remove(window,'scroll',s.scrollHandler);s.events.remove(window,'resize',s.resizeHandler);if (s.freezeOnBlur) {if (isIE) {s.events.remove(document,'focusout',s.freeze);s.events.remove(document,'focusin',s.resume);} else {s.events.remove(window,'blur',s.freeze);s.events.remove(window,'focus',s.resume);}}};this.show = function() {var i;for (i=this.flakes.length; i--;) {this.flakes[i].o.style.display = 'block';}};this.SnowFlake = function(parent,type,x,y) {var s = this, storm = parent;this.type = type;this.x = x||parseInt(rnd(screenX-20),10);this.y = (!isNaN(y)?y:-rnd(screenY)-12);this.vX = null;this.vY = null;this.vAmpTypes = [1,1.2,1.4,1.6,1.8]; // "amplification" for vX/vY (based on flake size/type)this.vAmp = this.vAmpTypes[this.type];this.melting = false;this.meltFrameCount = storm.meltFrameCount;this.meltFrames = storm.meltFrames;this.meltFrame = 0;this.twinkleFrame = 0;this.active = 1;this.fontSize = (10+(this.type/5)*10);this.o = document.createElement('div');this.o.innerHTML = storm.snowCharacter;this.o.style.color = storm.snowColor;this.o.style.position = (fixedForEverything?'fixed':'absolute');this.o.style.width = storm.flakeWidth+'px';this.o.style.height = storm.flakeHeight+'px';this.o.style.fontFamily = 'arial,verdana';this.o.style.cursor = 'default';this.o.style.overflow = 'hidden';this.o.style.fontWeight = 'normal';this.o.style.zIndex = storm.zIndex;docFrag.appendChild(this.o);this.refresh = function() {if (isNaN(s.x) || isNaN(s.y)) {// safety checkreturn false;}s.o.style.left = s.x+'px';s.o.style.top = s.y+'px';};this.stick = function() {if (noFixed || (storm.targetElement !== document.documentElement && storm.targetElement !== document.body)) {s.o.style.top = (screenY+scrollY-storm.flakeHeight)+'px';} else if (storm.flakeBottom) {s.o.style.top = storm.flakeBottom+'px';} else {s.o.style.display = 'none';s.o.style.top = 'auto';s.o.style.bottom = '0px';s.o.style.position = 'fixed';s.o.style.display = 'block';}};this.vCheck = function() {if (s.vX>=0 && s.vX<0.2) {s.vX = 0.2;} else if (s.vX<0 && s.vX>-0.2) {s.vX = -0.2;}if (s.vY>=0 && s.vY<0.2) {s.vY = 0.2;}};this.move = function() {var vX = s.vX*windOffset, yDiff;s.x += vX;s.y += (s.vY*s.vAmp);if (s.x >= screenX || screenX-s.x < storm.flakeWidth) { // X-axis scroll checks.x = 0;} else if (vX < 0 && s.x-storm.flakeLeftOffset < -storm.flakeWidth) {s.x = screenX-storm.flakeWidth-1; // flakeWidth;}s.refresh();yDiff = screenY+scrollY-s.y;if (yDiff<storm.flakeHeight) {s.active = 0;if (storm.snowStick) {s.stick();} else {s.recycle();}} else {if (storm.useMeltEffect && s.active && s.type < 3 && !s.melting && Math.random()>0.998) {// ~1/1000 chance of melting mid-air, with each frames.melting = true;s.melt();// only incrementally melt one frame// s.melting = false;}if (storm.useTwinkleEffect) {if (!s.twinkleFrame) {if (Math.random()>0.9) {s.twinkleFrame = parseInt(Math.random()*20,10);}} else {s.twinkleFrame--;s.o.style.visibility = (s.twinkleFrame && s.twinkleFrame%2===0?'hidden':'visible');}}}};this.animate = function() {// main animation loop// move, check status, die etc.s.move();};this.setVelocities = function() {s.vX = vRndX+rnd(storm.vMaxX*0.12,0.1);s.vY = vRndY+rnd(storm.vMaxY*0.12,0.1);};this.setOpacity = function(o,opacity) {if (!opacitySupported) {return false;}o.style.opacity = opacity;};this.melt = function() {if (!storm.useMeltEffect || !s.melting) {s.recycle();} else {if (s.meltFrame < s.meltFrameCount) {s.setOpacity(s.o,s.meltFrames[s.meltFrame]);s.o.style.fontSize = s.fontSize-(s.fontSize*(s.meltFrame/s.meltFrameCount))+'px';s.o.style.lineHeight = storm.flakeHeight+2+(storm.flakeHeight*0.75*(s.meltFrame/s.meltFrameCount))+'px';s.meltFrame++;} else {s.recycle();}}};this.recycle = function() {s.o.style.display = 'none';s.o.style.position = (fixedForEverything?'fixed':'absolute');s.o.style.bottom = 'auto';s.setVelocities();s.vCheck();s.meltFrame = 0;s.melting = false;s.setOpacity(s.o,1);s.o.style.padding = '0px';s.o.style.margin = '0px';s.o.style.fontSize = s.fontSize+'px';s.o.style.lineHeight = (storm.flakeHeight+2)+'px';s.o.style.textAlign = 'center';s.o.style.verticalAlign = 'baseline';s.x = parseInt(rnd(screenX-storm.flakeWidth-20),10);s.y = parseInt(rnd(screenY)*-1,10)-storm.flakeHeight;s.refresh();s.o.style.display = 'block';s.active = 1;};this.recycle(); // set up x/y coords etc.this.refresh();};this.snow = function() {var active = 0, used = 0, waiting = 0, flake = null, i;for (i=s.flakes.length; i--;) {if (s.flakes[i].active === 1) {s.flakes[i].move();active++;} else if (s.flakes[i].active === 0) {used++;} else {waiting++;}if (s.flakes[i].melting) {s.flakes[i].melt();}}if (active<s.flakesMaxActive) {flake = s.flakes[parseInt(rnd(s.flakes.length),10)];if (flake.active === 0) {flake.melting = true;}}};this.mouseMove = function(e) {if (!s.followMouse) {return true;}var x = parseInt(e.clientX,10);if (x<screenX2) {windOffset = -windMultiplier+(x/screenX2*windMultiplier);} else {x -= screenX2;windOffset = (x/screenX2)*windMultiplier;}};this.createSnow = function(limit,allowInactive) {var i;for (i=0; i<limit; i++) {s.flakes[s.flakes.length] = new s.SnowFlake(s,parseInt(rnd(flakeTypes),10));if (allowInactive || i>s.flakesMaxActive) {s.flakes[s.flakes.length-1].active = -1;}}storm.targetElement.appendChild(docFrag);};this.timerInit = function() {s.timers = (!isWin98?[setInterval(s.snow,s.animationInterval)]:[setInterval(s.snow,s.animationInterval*3),setInterval(s.snow,s.animationInterval)]);};this.init = function() {var i;for (i=0; i<s.meltFrameCount; i++) {s.meltFrames.push(1-(i/s.meltFrameCount));}s.randomizeWind();s.createSnow(s.flakesMax); // create initial batchs.events.add(window,'resize',s.resizeHandler);s.events.add(window,'scroll',s.scrollHandler);if (s.freezeOnBlur) {if (isIE) {s.events.add(document,'focusout',s.freeze);s.events.add(document,'focusin',s.resume);} else {s.events.add(window,'blur',s.freeze);s.events.add(window,'focus',s.resume);}}s.resizeHandler();s.scrollHandler();if (s.followMouse) {s.events.add(isIE?document:window,'mousemove',s.mouseMove);}s.animationInterval = Math.max(20,s.animationInterval);s.timerInit();};this.start = function(bFromOnLoad) {if (!didInit) {didInit = true;} else if (bFromOnLoad) {// already loaded and runningreturn true;}if (typeof s.targetElement === 'string') {var targetID = s.targetElement;s.targetElement = document.getElementById(targetID);if (!s.targetElement) {throw new Error('Snowstorm: Unable to get targetElement "'+targetID+'"');}}if (!s.targetElement) {s.targetElement = (!isIE?(document.documentElement?document.documentElement:document.body):document.body);}if (s.targetElement !== document.documentElement && s.targetElement !== document.body) {s.resizeHandler = s.resizeHandlerAlt; // re-map handler to get element instead of screen dimensions}s.resizeHandler(); // get bounding box elementss.usePositionFixed = (s.usePositionFixed && !noFixed); // whether or not position:fixed is supportedfixedForEverything = s.usePositionFixed;if (screenX && screenY && !s.disabled) {s.init();s.active = true;}};function doDelayedStart() {window.setTimeout(function() {s.start(true);}, 20);// event cleanups.events.remove(isIE?document:window,'mousemove',doDelayedStart);}function doStart() {if (!s.excludeMobile || !isMobile) {if (s.freezeOnBlur) {s.events.add(isIE?document:window,'mousemove',doDelayedStart);} else {doDelayedStart();}}// event cleanups.events.remove(window, 'load', doStart);}// hooks for starting the snowif (s.autoStart) {s.events.add(window, 'load', doStart, false);}return this;}(window, document));

index.htm

<html>
<head>
<script src="snowStorm.js"></script>
<style>
body{background-color: black;width::100%;height:500px;}
</style>
</head>
<body>
</body>
</html>

snowStorm.js下雪效果相关推荐

  1. canvas下雪效果(原生js)

    效果展示: 源码展示: <!doctype html> <html> <head><meta charset="utf-8">< ...

  2. js画布实现下雪效果

    之前使用css实现了下雪效果,也对比了前端多种实现动画的方法(链接见文末) 下面使用js和canvas的方法配合requestAnimationFrame实现一个简单的下雪效果,代码十分精简. 代码 ...

  3. 20多行js实现canvas雪夜下雪效果

    目录 前言 实现canvas雪夜下雪效果 1.设置canvas标签的id为cvs,设置背景颜色为黑色 2.设置body外边距为0 3.通过获取DOM元素获得画板 4.指定二维绘图 5.设置宽高填满页面 ...

  4. python下雪的实例_javascript实现下雪效果【实例代码】

    原理 : 1.js动态创建DIV,指定CLASS类设置不同的背景图样式显示不同的雪花效果. 2.js获取创建的DIV并改变其top属性值,当下落的高度大于屏幕高后删除该移动div 3.好像不够完善勿喷 ...

  5. JAVA圣诞代码_[Java教程]【Merry Christmas】圣诞节,给博客添加浪漫的下雪效果!...

    [Java教程][Merry Christmas]圣诞节,给博客添加浪漫的下雪效果! 0 2012-12-25 15:00:20 一年一度的圣诞节又到了,首先祝大家好运一串串,健康一年年,平安到永远! ...

  6. 纯css模拟下雪效果

    效果如其名,想必都见过下雪(可能南方人除外哈哈),但下雪效果只是一类效果的名称,可以是红包雨等一些自由落体的运动效果,本文就是用纯css模拟下雪的效果. 1.前言 由于公司产品的活动,需要模拟类似下雪 ...

  7. JS打字效果的动态菜单代码分享

    这篇文章主要介绍了JS打字效果的动态菜单,推荐给大家,有需要的小伙伴可以参考下. 这是一款基于javascript实现的打字效果的动态菜单特效代码,分享给大家学习学习. 小提示:浏览器中如果不能正常运 ...

  8. Js黑客帝国效果 文字下落 制作过程和思路

    效果预览: http://jsfiddle.net/dtdxrk/m8R6b/embedded/result/ Js黑客帝国效果 文字向下落制作过程和思路 1.css控制文字竖显示 2.动态添加div ...

  9. Bootstrap警告框、弹出提示层、模态框的js插件效果总结

    Bootstrap警告框js插件 经常会在bootstrap项目中遇到警告框.弹出提示层.弹出模态框组件等等场景应用. 使用前准备: 插件使用之前,请先导入如下文件: jquery.min.js bo ...

最新文章

  1. python自动翻译导学案_批量翻译踩过的坑--python
  2. 史上更全面的数据库分库分表、数据一致性、主键分配思路!
  3. 【深度学习】如何更好的Fit一个深度神经网络框架下的模型
  4. thread_t 数组 linux,首页 C#如何打印pthread_t
  5. VMware推出TrustPoint产品,完善终端用户计算方案
  6. 【DP】晨练计划(ybtoj)
  7. tail查看nohup.out文件内容
  8. SystemCenter2012SP1实践(12)服务器、网络和存储配置
  9. 无需训练 RNN 或生成模型,如何编写一个快速且通用的 AI “讲故事”项目?
  10. dell r230u盘启动安装2008_利用U盘安装win2008r2系统的步骤
  11. 什么是单页面应用SPA?和多页面应用的区别?
  12. linux cpu占用分析,Linux下CPU占用率高分析方法
  13. const char *p与char * const p区别
  14. CPA十二--我国外币会计报表折算(转载)
  15. 365天历史时间顺序读经计划表
  16. mysql倒序获取最新10条后正序展示
  17. 少儿编程培训 python
  18. 医院计算机人员考试试题,医院信息科考试试题及答案-
  19. 桌面上计算机图标移动变成复制,电脑桌面图标都变成lnk后缀怎么办
  20. Flutter 不一样的跨平台解决方案

热门文章

  1. mac 开机启动php,mac系统,php-fpm加入开机启动项
  2. iOS apple 登录
  3. 2023拼多多店铺分类id
  4. python训练Word2Vec词向量
  5. python两个csv表数据合并_python – 根据列中的数据合并两个CSV文件
  6. Error:1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL
  7. app中我的页面头像及背景效果实现
  8. java利用正则表达式提取字符串中的整数和小数部分
  9. 美国音乐学院计算机音乐专业排名2015年,美国音乐学院排名小提琴专业排名大全(本科)...
  10. python数据分析:商品数据化运营(上)——知识点