xiaoxingxing.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* 鼠标特效 - 小星星拖尾 */
  2. (function fairyDustCursor() {
  3. var possibleColors = ["#D61C59", "#E7D84B", "#1B8798"]
  4. var width = window.innerWidth;
  5. var height = window.innerHeight;
  6. var cursor = {
  7. x: width / 2,
  8. y: width / 2
  9. };
  10. var particles = [];
  11. function init() {
  12. bindEvents();
  13. loop();
  14. }
  15. // Bind events that are needed
  16. function bindEvents() {
  17. document.addEventListener('mousemove', onMouseMove);
  18. window.addEventListener('resize', onWindowResize);
  19. }
  20. function onWindowResize(e) {
  21. width = window.innerWidth;
  22. height = window.innerHeight;
  23. }
  24. function onMouseMove(e) {
  25. cursor.x = e.clientX;
  26. cursor.y = e.clientY;
  27. addParticle(cursor.x, cursor.y, possibleColors[Math.floor(Math.random() * possibleColors.length)]);
  28. }
  29. function addParticle(x, y, color) {
  30. var particle = new Particle();
  31. particle.init(x, y, color);
  32. particles.push(particle);
  33. }
  34. function updateParticles() {
  35. // Updated
  36. for (var i = 0; i < particles.length; i++) {
  37. particles[i].update();
  38. }
  39. // Remove dead particles
  40. for (var i = particles.length - 1; i >= 0; i--) {
  41. if (particles[i].lifeSpan < 0) {
  42. particles[i].die();
  43. particles.splice(i, 1);
  44. }
  45. }
  46. }
  47. function loop() {
  48. requestAnimationFrame(loop);
  49. updateParticles();
  50. }
  51. /**
  52. * Particles
  53. */
  54. function Particle() {
  55. this.character = "*";
  56. this.lifeSpan = 120; //ms
  57. this.initialStyles = {
  58. "position": "fixed",
  59. "display": "inline-block",
  60. "top": "0px",
  61. "left": "0px",
  62. "pointerEvents": "none",
  63. "touch-action": "none",
  64. "z-index": "10000000",
  65. "fontSize": "25px",
  66. "will-change": "transform"
  67. };
  68. // Init, and set properties
  69. this.init = function(x, y, color) {
  70. this.velocity = {
  71. x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 2),
  72. y: 1
  73. };
  74. this.position = {
  75. x: x + 10,
  76. y: y + 10
  77. };
  78. this.initialStyles.color = color;
  79. this.element = document.createElement('span');
  80. this.element.innerHTML = this.character;
  81. applyProperties(this.element, this.initialStyles);
  82. this.update();
  83. document.querySelector('.js-cursor-container').appendChild(this.element);
  84. };
  85. this.update = function() {
  86. this.position.x += this.velocity.x;
  87. this.position.y += this.velocity.y;
  88. this.lifeSpan--;
  89. this.element.style.transform = "translate3d(" + this.position.x + "px," + this.position.y + "px, 0) scale(" + (this.lifeSpan / 120) + ")";
  90. }
  91. this.die = function() {
  92. this.element.parentNode.removeChild(this.element);
  93. }
  94. }
  95. /**
  96. * Utils
  97. */
  98. // Applies css `properties` to an element.
  99. function applyProperties(target, properties) {
  100. for (var key in properties) {
  101. target.style[key] = properties[key];
  102. }
  103. }
  104. if (!('ontouchstart' in window || navigator.msMaxTouchPoints)) init();
  105. })();