欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

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

时间:2020-10-29 10:57:34|栏目:JavaScript代码|点击:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<style type="text/css">
.canvas{
width:500px;
height:500px;
position:relative;
}
</style>
</head>
<body>
<div>
<input type="button" onclick="javascript:falling.init();" value="GO" />
</div>
<div class="canvas" id="canvas"></div>
<script type="text/javascript">
/*
*坠落效果
*/
function Falling(){
 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"];
 this.canvas=$("#canvas");
 this.step=15;
 this.freq=10;
 this.height=500;
 this.width=500;
 this.si=null;
}
Falling.prototype={
 fallingAction:function(dom){
  var self=this;
  var freqs=[10,15,20];//每次下落的距离
  var disS=[];//记录所有dom的当前距离
  var disPerFreqS=[];//每个dom的
  var targetDis=500;
  var domCssTopS=[];//所有dom的top属性
  var successDom=[];//记录哪些dom已经结束运动
  var successCount=0;//有多少个dom已经结束
  var total=dom.length;
  var freqMarkLength=freqs.length;
  for(var i=0,j=dom.length;i<j;i++){
   domCssTopS[i]=dom[i].position().top;
   disS[i]=0;
   disPerFreqS[i]=freqs[parseInt(Math.random()*freqMarkLength)];
  }
  self.si=setInterval(function(){
   if(successCount>=total){
    clearInterval(self.si);
   }
   for(var i=0,j=dom.length;i<j;i++){
    if(typeof(successDom[i])!="undefined" && successDom[i]=="ok"){
     continue;
    }
    disS[i] += disPerFreqS[i];
    if(disS[i] >= targetDis){
     dom[i].css("top", targetDis+domCssTopS[i]);
     successDom[i]="ok";
     successCount++;;
    }else{
     dom[i].css("top", disS[i]+domCssTopS[i]);
    }
   }
  },self.freq);
 },
 init:function(){
  var self=this;
  self.canvas.html('');
  var dom=[];
  var l=0;
  var t=0;
  var tempDom=$("<div style='position;absolute;left:-100000;visibility:hidden'></div>").appendTo($("body"));
  for(var i=0,j=self.dict.length;i<j;i++){
   dom[i]=$("<span style='position:absolute'>"+self.dict[i]+"</span>").appendTo(tempDom);
   var domWidth=dom[i].width();
   var domHeight=dom[i].height();
   if(t<self.height){
    if(l<self.width){
     if(domWidth+l<=self.width){
      dom[i].css({"top":t,"left":l});
      self.canvas.append(dom[i]);
      l += dom[i].width();
     }else{
      if(domHeight+t<=self.height){
       t=t+domHeight;
       dom[i].css({"top":t,"left":0});
       self.canvas.append(dom[i]);
       l = dom[i].width();
      }else{
       break;//到极限了
      }
     }
    }else{
      if(domHeight+t<=self.height){
       t=t+domHeight;
       l=0;
       dom[i].css({"top":t,"left":l});
       self.canvas.append(dom[i]);
      }else{
       break;//到极限了
      }
    }
   }//else极限
  }
  /*
  for(var i=0,l=self.dict.length;i<l;i++){
   self.fallingAction(dom[i]);
  }
  */
  self.fallingAction(dom);
 }
}
var falling=new Falling();
falling.init();
</script>
</body>
</html>

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

上一篇:微信小程序实现单选选项卡切换效果

栏    目:JavaScript代码

下一篇:CountUp.js实现数字滚动增值效果

本文标题:JS实现文字掉落效果的方法

本文地址:http://www.codeinn.net/misctech/16871.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有