javascript – 随机自然运动jquery

前端之家收集整理的这篇文章主要介绍了javascript – 随机自然运动jquery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用 jquery为图像重新创建此类型的移动: http://www.istockphoto.com/stock-video-12805249-moving-particles-loop-soft-green-hd-1080.php

我打算将它用作网页背景.如果jquery不可能,我将使用flash as3.但我更喜欢jquery.

解决方法

编辑: Raphael肯定更适合这个,因为它支持IE. jQuery的问题在于,由于CSS的限制,圆角在IE中很难做到……在Raphael交叉浏览器圈子里没有汗水.

jsFiddle with Raphael – all browsers

(虽然它可能看起来更好speeded up in IE)

  1. (function() {
  2. var paper,circs,i,nowX,nowY,timer,props = {},toggler = 0,elie,dx,dy,rad,cur,opa;
  3. // Returns a random integer between min and max
  4. // Using Math.round() will give you a non-uniform distribution!
  5. function ran(min,max)
  6. {
  7. return Math.floor(Math.random() * (max - min + 1)) + min;
  8. }
  9.  
  10. function moveIt()
  11. {
  12. for(i = 0; i < circs.length; ++i)
  13. {
  14. // Reset when time is at zero
  15. if (! circs[i].time)
  16. {
  17. circs[i].time = ran(30,100);
  18. circs[i].deg = ran(-179,180);
  19. circs[i].vel = ran(1,5);
  20. circs[i].curve = ran(0,1);
  21. circs[i].fade = ran(0,1);
  22. circs[i].grow = ran(-2,2);
  23. }
  24. // Get position
  25. nowX = circs[i].attr("cx");
  26. nowY = circs[i].attr("cy");
  27. // Calc movement
  28. dx = circs[i].vel * Math.cos(circs[i].deg * Math.PI/180);
  29. dy = circs[i].vel * Math.sin(circs[i].deg * Math.PI/180);
  30. // Calc new position
  31. nowX += dx;
  32. nowY += dy;
  33. // Calc wrap around
  34. if (nowX < 0) nowX = 490 + nowX;
  35. else nowX = nowX % 490;
  36. if (nowY < 0) nowY = 490 + nowY;
  37. else nowY = nowY % 490;
  38.  
  39. // Render moved particle
  40. circs[i].attr({cx: nowX,cy: nowY});
  41.  
  42. // Calc growth
  43. rad = circs[i].attr("r");
  44. if (circs[i].grow > 0) circs[i].attr("r",Math.min(30,rad + .1));
  45. else circs[i].attr("r",Math.max(10,rad - .1));
  46.  
  47. // Calc curve
  48. if (circs[i].curve > 0) circs[i].deg = circs[i].deg + 2;
  49. else circs[i].deg = circs[i].deg - 2;
  50.  
  51. // Calc opacity
  52. opa = circs[i].attr("fill-opacity");
  53. if (circs[i].fade > 0) {
  54. circs[i].attr("fill-opacity",Math.max(.3,opa - .01));
  55. circs[i].attr("stroke-opacity",opa - .01)); }
  56. else {
  57. circs[i].attr("fill-opacity",Math.min(1,opa + .01));
  58. circs[i].attr("stroke-opacity",opa + .01)); }
  59.  
  60. // Progress timer for particle
  61. circs[i].time = circs[i].time - 1;
  62.  
  63. // Calc damping
  64. if (circs[i].vel < 1) circs[i].time = 0;
  65. else circs[i].vel = circs[i].vel - .05;
  66.  
  67. }
  68. timer = setTimeout(moveIt,60);
  69. }
  70.  
  71. window.onload = function () {
  72. paper = Raphael("canvas",500,500);
  73. circs = paper.set();
  74. for (i = 0; i < 30; ++i)
  75. {
  76. opa = ran(3,10)/10;
  77. circs.push(paper.circle(ran(0,500),ran(0,ran(10,30)).attr({"fill-opacity": opa,"stroke-opacity": opa}));
  78. }
  79. circs.attr({fill: "#00DDAA",stroke: "#00DDAA"});
  80. moveIt();
  81. elie = document.getElementById("toggle");
  82. elie.onclick = function() {
  83. (toggler++ % 2) ? (function(){
  84. moveIt();
  85. elie.value = " Stop ";
  86. }()) : (function(){
  87. clearTimeout(timer);
  88. elie.value = " Start ";
  89. }());
  90. }
  91. };
  92. }());​

第一次尝试jQuery解决方案如下:

这个jQuery尝试在IE中几乎失败,在FF中很慢. Chrome和Safari做得很好:

jsFiddle example for all browsers (IE is not that good)

(我没有在IE中实现淡入淡出,而且IE没有圆角…而且JS也比较慢,所以整体看起来很糟糕)

jsFiddle example for Chrome and Safari only (4x more particles)

  1. (function() {
  2. var x,y,$elie,pos,$that,vel,deg,fade,curve,ko,mo,oo,grow,len;
  3.  
  4. // Returns a random integer between min and max
  5. // Using Math.round() will give you a non-uniform distribution!
  6. function ran(min,max)
  7. {
  8. return Math.floor(Math.random() * (max - min + 1)) + min;
  9. }
  10.  
  11. function moveIt()
  12. {
  13. $("div.spec").each(function(i,v) {
  14. $elie = $(v);
  15. if (! $elie.data("time"))
  16. {
  17. $elie.data("time",ran(30,100));
  18. $elie.data("deg",ran(-179,180));
  19. $elie.data("vel",ran(3,10));
  20. $elie.data("curve",1));
  21. $elie.data("fade",1));
  22. $elie.data("grow",ran(-2,2));
  23. }
  24.  
  25. vel = $elie.data("vel");
  26. deg = $elie.data("deg");
  27. fade = $elie.data("fade");
  28. curve = $elie.data("curve");
  29. grow = $elie.data("grow");
  30.  
  31. len = $elie.width();
  32. if (grow > 0)
  33. len = Math.min(len + grow,50);
  34. else
  35. len = Math.max(len + grow,20);
  36.  
  37. $elie.css("-moz-border-radius",len/2);
  38. $elie.css("border-radius",len/2);
  39.  
  40. $elie.css("width",len);
  41. $elie.css("height",len);
  42.  
  43. pos = $elie.position();
  44.  
  45. $elie.data("time",$elie.data("time") - 1);
  46.  
  47. if (curve)
  48. $elie.data("deg",(deg + 5) % 180);
  49. else
  50. $elie.data("deg",(deg - 5) % 180);
  51.  
  52. ko = $elie.css("-khtml-opacity");
  53. mo = $elie.css("-moz-opacity");
  54. oo = $elie.css("opacity");
  55. if (fade)
  56. {
  57. $elie.css("-khtml-opacity",Math.max(ko - .1,.5));
  58. $elie.css("-moz-opacity",Math.max(mo - .1,.5));
  59. $elie.css("opacity",Math.max(oo - .1,.5));
  60. } else
  61. {
  62. $elie.css("-khtml-opacity",Math.min(ko - -.1,1));
  63. $elie.css("-moz-opacity",Math.min(mo - -.1,1));
  64. $elie.css("opacity",Math.min(oo - -.1,1));
  65. }
  66.  
  67. if (vel < 3)
  68. $elie.data("time",0);
  69. else
  70. $elie.data("vel",vel - .2);
  71.  
  72.  
  73. nowX = pos.left;
  74. nowY = pos.top;
  75.  
  76. x = vel * Math.cos(deg * Math.PI/180);
  77. y = vel * Math.sin(deg * Math.PI/180);
  78.  
  79. nowX = nowX + x;
  80. nowY = nowY + y;
  81.  
  82. if (nowX < 0)
  83. nowX = 490 + nowX;
  84. else
  85. nowX = nowX % 490;
  86.  
  87. if (nowY < 0)
  88. nowY = 490 + nowY;
  89. else
  90. nowY = nowY % 490;
  91. $elie.css("left",nowX);
  92. $elie.css("top",nowY);
  93. });
  94. }
  95. $(function() {
  96. $(document.createElement('div')).appendTo('body').attr('id','Box');
  97. $elie = $("<div/>").attr("class","spec");
  98. // Note that math random is inclussive for 0 and exclussive for Max
  99. for (i = 0; i < 100; ++i)
  100. {
  101. $that = $elie.clone();
  102. $that.css("top",495));
  103. $that.css("left",495));
  104. $("#Box").append($that);
  105. }
  106. timer = setInterval(moveIt,60);
  107. $("input").toggle(function() {
  108. clearInterval(timer);
  109. this.value = " Start ";
  110. },function() {
  111. timer = setInterval(moveIt,60);
  112. this.value = " Stop ";
  113. });
  114. });
  115. }());

猜你在找的jQuery相关文章