首页 > 语言 > JavaScript > 正文

JS实现文字掉落效果的方法

2024-05-06 16:19:30
字体:
来源:转载
供稿:网友

这篇文章主要介绍了JS实现文字掉落效果的方法,可实现文字往下掉落最终排序正常显示的效果,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JS实现文字掉落效果的方法。分享给大家供大家参考。具体实现方法如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru"
  4. <head> 
  5. <title></title> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  7. <script src="jquery-1.6.2.min.js" type="text/javascript"></script> 
  8. <style type="text/css"
  9. .canvas{ 
  10. width:500px; 
  11. height:500px; 
  12. position:relative; 
  13. </style> 
  14. </head> 
  15. <body> 
  16. <div> 
  17. <input type="button" onclick="javascript:falling.init();" value="GO" /> 
  18. </div> 
  19. <div class="canvas" id="canvas"></div> 
  20. <script type="text/javascript"
  21. /* 
  22. *坠落效果 
  23. */ 
  24. function Falling(){ 
  25. this.dict=["abcd","2222","sign","next","container","content","last","break","less","than","that","absolute","relative","my","index","html","java","c#","web","javascript","php","include","shit","bull","big","smart","call","apply","callee","caller","function"]; 
  26. this.canvas=$("#canvas"); 
  27. this.step=15; 
  28. this.freq=10; 
  29. this.height=500; 
  30. this.width=500; 
  31. this.si=null
  32. Falling.prototype={ 
  33. fallingAction:function(dom){ 
  34. var self=this
  35. var freqs=[10,15,20];//每次下落的距离 
  36. var disS=[];//记录所有dom的当前距离 
  37. var disPerFreqS=[];//每个dom的 
  38. var targetDis=500; 
  39. var domCssTopS=[];//所有dom的top属性 
  40. var successDom=[];//记录哪些dom已经结束运动 
  41. var successCount=0;//有多少个dom已经结束 
  42. var total=dom.length; 
  43. var freqMarkLength=freqs.length; 
  44. for(var i=0,j=dom.length;i<j;i++){ 
  45. domCssTopS[i]=dom[i].position().top; 
  46. disS[i]=0; 
  47. disPerFreqS[i]=freqs[parseInt(Math.random()*freqMarkLength)]; 
  48. self.si=setInterval(function(){ 
  49. if(successCount>=total){ 
  50. clearInterval(self.si); 
  51. for(var i=0,j=dom.length;i<j;i++){ 
  52. if(typeof(successDom[i])!="undefined" && successDom[i]=="ok"){ 
  53. continue
  54. disS[i] += disPerFreqS[i]; 
  55. if(disS[i] >= targetDis){ 
  56. dom[i].css("top", targetDis+domCssTopS[i]); 
  57. successDom[i]="ok"
  58. successCount++;; 
  59. }else
  60. dom[i].css("top", disS[i]+domCssTopS[i]); 
  61. },self.freq); 
  62. }, 
  63. init:function(){ 
  64. var self=this
  65. self.canvas.html(''); 
  66. var dom=[]; 
  67. var l=0; 
  68. var t=0; 
  69. var tempDom=$("<div style='position;absolute;left:-100000;visibility:hidden'></div>").appendTo($("body")); 
  70. for(var i=0,j=self.dict.length;i<j;i++){ 
  71. dom[i]=$("<span style='position:absolute'>"+self.dict[i]+"</span>").appendTo(tempDom); 
  72. var domWidth=dom[i].width(); 
  73. var domHeight=dom[i].height(); 
  74. if(t<self.height){ 
  75. if(l<self.width){ 
  76. if(domWidth+l<=self.width){ 
  77. dom[i].css({"top":t,"left":l}); 
  78. self.canvas.append(dom[i]); 
  79. l += dom[i].width(); 
  80. }else
  81. if(domHeight+t<=self.height){ 
  82. t=t+domHeight; 
  83. dom[i].css({"top":t,"left":0}); 
  84. self.canvas.append(dom[i]); 
  85. l = dom[i].width(); 
  86. }else
  87. break;//到极限了 
  88. }else
  89. if(domHeight+t<=self.height){ 
  90. t=t+domHeight; 
  91. l=0; 
  92. dom[i].css({"top":t,"left":l}); 
  93. self.canvas.append(dom[i]); 
  94. }else
  95. break;//到极限了 
  96. }//else极限 
  97. /* 
  98. for(var i=0,l=self.dict.length;i<l;i++){ 
  99. self.fallingAction(dom[i]); 
  100. } 
  101. */ 
  102. self.fallingAction(dom); 
  103. var falling=new Falling(); 
  104. falling.init(); 
  105. </script> 
  106. </body> 
  107. </html> 


希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选