Hiệu ứng tuyết rơi đơn giản bằng JavaScript

18.12.2016 / 23:09
hanhphucao
Bài đăng: 2470
Admin
Admin là người tận tâm và luôn hành xử đúng mực.

Code hiệu ứng tuyết rơi đơn giản trên trang sử dụng JavaScript. Mùa đông có tuyết rơi hay mùa Christmas đã đến :49:.

Hình ảnh:

[IMAGE]

Code:

JAVASCRIPT
  1. <script>
  2. // Hình bông tuyết tùy ý (hình dáng)
  3. var requestAnimationFrame = window.requestAnimationFrame ||
  4. window.mozRequestAnimationFrame ||
  5. window.webkitRequestAnimationFrame ||
  6. window.msRequestAnimationFrame;
  7.  
  8. var transforms = ["transform",
  9. "msTransform",
  10. "webkitTransform",
  11. "mozTransform",
  12. "oTransform"];
  13.  
  14. var transformProperty = getSupportedPropertyName(transforms);
  15.  
  16. // Mảng lưu trữ Snowflake objects
  17. var snowflakes = [];
  18.  
  19. // Khai báo biến lưu trữ kích thước cửa sổ
  20. var browserWidth;
  21. var browserHeight;
  22.  
  23. // Số bông tuyết rơi
  24. var numberOfSnowflakes = 50;
  25.  
  26. // Thiết lập lại vị trí các bông tuyết
  27. var resetPosition = false;
  28.  
  29. //
  30. // Bắt đầu nào :)...
  31. //
  32. function setup() {
  33. window.addEventListener("DOMContentLoaded", generateSnowflakes, false);
  34. window.addEventListener("resize", setResetFlag, false);
  35. }
  36. setup();
  37.  
  38. //
  39. // Properties
  40. //
  41. function getSupportedPropertyName(properties) {
  42. for (var i = 0; i < properties.length; i++) {
  43. if (typeof document.body.style[properties[i]] != "undefined") {
  44. return properties[i];
  45. }
  46. }
  47. return null;
  48. }
  49.  
  50. //
  51. // Cấu trúc Snowflake object
  52. //
  53. function Snowflake(element, radius, speed, xPos, yPos) {
  54.  
  55. // Thiết lập thuộc tính bông tuyết
  56. this.element = element;
  57. this.radius = radius;
  58. this.speed = speed;
  59. this.xPos = xPos;
  60. this.yPos = yPos;
  61.  
  62. // Khai báo biến bông tuyết chuyển động
  63. this.counter = 0;
  64. this.sign = Math.random() < 0.5 ? 1 : -1;
  65.  
  66. // Thiết lập giá trị opacity (độ trong suốt) và kích thước bông tuyết
  67. this.element.style.opacity = .1 + Math.random();
  68. this.element.style.fontSize = 12 + Math.random() * 50 + "px";
  69. }
  70.  
  71. //
  72. // function điều khiển chuyển động bông tuyết
  73. //
  74. Snowflake.prototype.update = function () {
  75.  
  76. // Sử dụng tính toán tìm vị trí x và y
  77. this.counter += this.speed / 5000;
  78. this.xPos += this.sign * this.speed * Math.cos(this.counter) / 40;
  79. this.yPos += Math.sin(this.counter) / 40 + this.speed / 30;
  80.  
  81. // Thiết lập vị trí bông tuyết
  82. setTranslate3DTransform(this.element, Math.round(this.xPos), Math.round(this.yPos));
  83.  
  84. // Nếu bông tuyết rơi xuống ngoài cửa sổ, điều khiển bông tuyết quay lại từ bên trên
  85. if (this.yPos > browserHeight) {
  86. this.yPos = -50;
  87. }
  88. }
  89.  
  90. //
  91. // Cách khác để thiết lập vị trí bông tuyết
  92. //
  93. function setTranslate3DTransform(element, xPosition, yPosition) {
  94. var val = "translate3d(" + xPosition + "px, " + yPosition + "px" + ", 0)";
  95. element.style[transformProperty] = val;
  96. }
  97.  
  98. //
  99. // function tạo tuyết
  100. //
  101. function generateSnowflakes() {
  102.  
  103. // Lấy đối tượng bông tuyết và lưu lại
  104. var originalSnowflake = document.querySelector(".snowflake");
  105.  
  106. // Truy cập vào nhớ tạm
  107. var snowflakeContainer = originalSnowflake.parentNode;
  108.  
  109. // Lấy kích thước cửa sổ
  110. browserWidth = document.documentElement.clientWidth;
  111. browserHeight = document.documentElement.clientHeight;
  112.  
  113. // Dùng hàm for để tạo ra từng bông tuyết
  114. for (var i = 0; i < numberOfSnowflakes; i++) {
  115.  
  116. // Sao chép bông tuyết ra thành nhiều bản và lưu vào biến snowflakeContainer
  117. var snowflakeCopy = originalSnowflake.cloneNode(true);
  118. snowflakeContainer.appendChild(snowflakeCopy);
  119.  
  120. // Thiết lập thuộc tính vị trí ban đầu và vị trí xung quanh của bông tuyết
  121. var initialXPos = getPosition(50, browserWidth);
  122. var initialYPos = getPosition(50, browserHeight);
  123. var speed = 5+Math.random()*40;
  124. var radius = 4+Math.random()*10;
  125.  
  126. // Tạo Snowflake object
  127. var snowflakeObject = new Snowflake(snowflakeCopy,
  128. radius,
  129. speed,
  130. initialXPos,
  131. initialYPos);
  132. snowflakes.push(snowflakeObject);
  133. }
  134.  
  135. // Loại bỏ các bông tuyết từ từ (tạo hiệu ứng bông tuyết mờ dần)
  136. snowflakeContainer.removeChild(originalSnowflake);
  137.  
  138. // gọi lại function moveSnowflakes sau 0.03 giây
  139. moveSnowflakes();
  140. }
  141.  
  142. //
  143. // function di chuyển các bông tuyết
  144. //
  145. function moveSnowflakes() {
  146. for (var i = 0; i < snowflakes.length; i++) {
  147. var snowflake = snowflakes[i];
  148. snowflake.update();
  149. }
  150.  
  151. // Đặt lại vị trí tất cả bông tuyết đến một giá trị mới
  152. if (resetPosition) {
  153. browserWidth = document.documentElement.clientWidth;
  154. browserHeight = document.documentElement.clientHeight;
  155.  
  156. for (var i = 0; i < snowflakes.length; i++) {
  157. var snowflake = snowflakes[i];
  158.  
  159. snowflake.xPos = getPosition(50, browserWidth);
  160. snowflake.yPos = getPosition(50, browserHeight);
  161. }
  162.  
  163. resetPosition = false;
  164. }
  165.  
  166. requestAnimationFrame(moveSnowflakes);
  167. }
  168.  
  169. //
  170. // function return một số giữa (maximum - offset) và (maximum + offset)
  171. // offset: lấy tọa độ element
  172. //
  173. function getPosition(offset, size) {
  174. return Math.round(-1*offset + Math.random() * (size+2*offset));
  175. }
  176.  
  177. //
  178. // function thiết lập lại vị trí tất cả bông tuyết
  179. //
  180. function setResetFlag(e) {
  181. resetPosition = true;
  182. }</script>

Thêm CSS:

CSS
  1. <style>
  2. #snowflakeContainer {
  3. position: absolute;
  4. left: 0px;
  5. top: 0px;
  6. }
  7. .snowflake {
  8. padding-left: 15px;
  9. font-family: Cambria, Georgia, serif;
  10. font-size: 14px;
  11. line-height: 24px;
  12. position: fixed;
  13. color: #FFFFFF;
  14. user-select: none;
  15. z-index: 1000;
  16. }
  17. .snowflake:hover {
  18. cursor: default;
  19. }</style>

Thêm HTML:

HTML5
  1. <div id="snowflakeContainer">
  2. <p class="snowflake">*</p>
  3. </div>

Chúc các bạn thành công!

Source: PhoNho.Net.

Đã chỉnh sửa. hanhphucao (18.12.2016 / 23:14)
18.12.2016 / 23:11
MrKen
Bài đăng: 2653
Trùm!
Vẫn là A N H

like nhưng chưa dùng =))

19.12.2016 / 03:07
Jichan
Bài đăng: 1028
Member
Mệt cứ nói, đừng cố....

Cái này em có 2 năm trước :3

19.12.2016 / 09:03
nasic
Bài đăng: 784
Member
Ma Văn Nguyên

Mùa mưa lũ không cần tuyết rơi :11: