js实现下雪雪花特效

圣诞节了,码农的浪漫,能用代码,就尽量用代码解决。

而使用Spimes 主题做的网站,要实现下雪的特效,几秒钟就能完事。

  1. <script type="text/javascript">
  2. (function($){
  3. $.fn.snow = function(options){
  4. var $flake = $('<div id="snowbox" />').css({'position': 'absolute','z-index':'9999', 'top': '-50px'}).html('&#10052;'),
  5. documentHeight = $(document).height(),
  6. documentWidth = $(document).width(),
  7. defaults = {
  8. minSize : 10,
  9. maxSize : 20,
  10. newOn : 1000,
  11. flakeColor : "#AFDAEF" /* 此处可以定义雪花颜色,若要白色可以改为#FFFFFF */
  12. },
  13. options = $.extend({}, defaults, options);
  14. var interval= setInterval( function(){
  15. var startPositionLeft = Math.random() * documentWidth - 100,
  16. startOpacity = 0.5 + Math.random(),
  17. sizeFlake = options.minSize + Math.random() * options.maxSize,
  18. endPositionTop = documentHeight - 200,
  19. endPositionLeft = startPositionLeft - 500 + Math.random() * 500,
  20. durationFall = documentHeight * 10 + Math.random() * 5000;
  21. $flake.clone().appendTo('body').css({
  22. left: startPositionLeft,
  23. opacity: startOpacity,
  24. 'font-size': sizeFlake,
  25. color: options.flakeColor
  26. }).animate({
  27. top: endPositionTop,
  28. left: endPositionLeft,
  29. opacity: 0.2
  30. },durationFall,'linear',function(){
  31. $(this).remove()
  32. });
  33. }, options.newOn);
  34. };
  35. })(jQuery);
  36. $(function(){
  37. $.fn.snow({
  38. minSize: 5, /* 定义雪花最小尺寸 */
  39. maxSize: 50,/* 定义雪花最大尺寸 */
  40. newOn: 300 /* 定义密集程度,数字越小越密集 */
  41. });
  42. });
  43. </script>

第二种,个人觉得第一种更好看点

  1. <script type="text/javascript">
  2. /* 控制下雪 */
  3. function snowFall(snow) {
  4. /* 可配置属性 */
  5. snow = snow || {};
  6. this.maxFlake = snow.maxFlake || 200; /* 最多片数 */
  7. this.flakeSize = snow.flakeSize || 10; /* 雪花形状 */
  8. this.fallSpeed = snow.fallSpeed || 1; /* 坠落速度 */
  9. }
  10. /* 兼容写法 */
  11. requestAnimationFrame = window.requestAnimationFrame ||
  12. window.mozRequestAnimationFrame ||
  13. window.webkitRequestAnimationFrame ||
  14. window.msRequestAnimationFrame ||
  15. window.oRequestAnimationFrame ||
  16. function(callback) { setTimeout(callback, 1000 / 60); };
  17. cancelAnimationFrame = window.cancelAnimationFrame ||
  18. window.mozCancelAnimationFrame ||
  19. window.webkitCancelAnimationFrame ||
  20. window.msCancelAnimationFrame ||
  21. window.oCancelAnimationFrame;
  22. /* 开始下雪 */
  23. snowFall.prototype.start = function(){
  24. /* 创建画布 */
  25. snowCanvas.apply(this);
  26. /* 创建雪花形状 */
  27. createFlakes.apply(this);
  28. /* 画雪 */
  29. drawSnow.apply(this)
  30. }
  31. /* 创建画布 */
  32. function snowCanvas() {
  33. /* 添加Dom结点 */
  34. var snowcanvas = document.createElement("canvas");
  35. snowcanvas.id = "snowfall";
  36. snowcanvas.width = window.innerWidth;
  37. snowcanvas.height = document.body.clientHeight;
  38. snowcanvas.setAttribute("style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");
  39. document.getElementsByTagName("body")[0].appendChild(snowcanvas);
  40. this.canvas = snowcanvas;
  41. this.ctx = snowcanvas.getContext("2d");
  42. /* 窗口大小改变的处理 */
  43. window.onresize = function() {
  44. snowcanvas.width = window.innerWidth;
  45. /* snowcanvas.height = window.innerHeight */
  46. }
  47. }
  48. /* 雪运动对象 */
  49. function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
  50. this.x = Math.floor(Math.random() * canvasWidth); /* x坐标 */
  51. this.y = Math.floor(Math.random() * canvasHeight); /* y坐标 */
  52. this.size = Math.random() * flakeSize + 2; /* 形状 */
  53. this.maxSize = flakeSize; /* 最大形状 */
  54. this.speed = Math.random() * 1 + fallSpeed; /* 坠落速度 */
  55. this.fallSpeed = fallSpeed; /* 坠落速度 */
  56. this.velY = this.speed; /* Y方向速度 */
  57. this.velX = 0; /* X方向速度 */
  58. this.stepSize = Math.random() / 30; /* 步长 */
  59. this.step = 0 /* 步数 */
  60. }
  61. flakeMove.prototype.update = function() {
  62. var x = this.x,
  63. y = this.y;
  64. /* 左右摆动(余弦) */
  65. this.velX *= 0.98;
  66. if (this.velY <= this.speed) {
  67. this.velY = this.speed
  68. }
  69. this.velX += Math.cos(this.step += .05) * this.stepSize;
  70. this.y += this.velY;
  71. this.x += this.velX;
  72. /* 飞出边界的处理 */
  73. if (this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {
  74. this.reset(canvas.width, canvas.height)
  75. }
  76. };
  77. /* 飞出边界-放置最顶端继续坠落 */
  78. flakeMove.prototype.reset = function(width, height) {
  79. this.x = Math.floor(Math.random() * width);
  80. this.y = 0;
  81. this.size = Math.random() * this.maxSize + 2;
  82. this.speed = Math.random() * 1 + this.fallSpeed;
  83. this.velY = this.speed;
  84. this.velX = 0;
  85. };
  86. // 渲染雪花-随机形状(此处可修改雪花颜色!!!)
  87. flakeMove.prototype.render = function(ctx) {
  88. var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
  89. snowFlake.addColorStop(0, "rgba(255, 255, 255, 0.9)"); /* 此处是雪花颜色,默认是白色 */
  90. snowFlake.addColorStop(.5, "rgba(255, 255, 255, 0.5)"); /* 若要改为其他颜色,请自行查 */
  91. snowFlake.addColorStop(1, "rgba(255, 255, 255, 0)"); /*16进制的RGB 颜色代码。 */
  92. ctx.save();
  93. ctx.fillStyle = snowFlake;
  94. ctx.beginPath();
  95. ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
  96. ctx.fill();
  97. ctx.restore();
  98. };
  99. /* 创建雪花-定义形状 */
  100. function createFlakes() {
  101. var maxFlake = this.maxFlake,
  102. flakes = this.flakes = [],
  103. canvas = this.canvas;
  104. for (var i = 0; i < maxFlake; i++) {
  105. flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))
  106. }
  107. }
  108. /* 画雪 */
  109. function drawSnow() {
  110. var maxFlake = this.maxFlake,
  111. flakes = this.flakes;
  112. ctx = this.ctx, canvas = this.canvas, that = this;
  113. /* 清空雪花 */
  114. ctx.clearRect(0, 0, canvas.width, canvas.height);
  115. for (var e = 0; e < maxFlake; e++) {
  116. flakes[e].update();
  117. flakes[e].render(ctx);
  118. }
  119. /* 一帧一帧的画 */
  120. this.loop = requestAnimationFrame(function() {
  121. drawSnow.apply(that);
  122. });
  123. }
  124. /* 调用及控制方法 */
  125. var snow = new snowFall({maxFlake:500});
  126. snow.start();
  127. </script>
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/135
2 条评论
4.5k

发表评论

已有 2 条评论

  1. 憨憨熊     Win 10 /    Chrome
    2020-08-17 11:30

    还是挺好玩的啊

  2. Muze     Win 10 /    Chrome
    2020-03-28 16:52

    我在wordpress2017主题中尝试了第二种方法,有效果,但是会把页面拉宽

!