JS實(shí)現(xiàn)文字掉落效果的方法
更新時間:2015年05月06日 17:46:10 投稿:shichen2014
這篇文章主要介紹了JS實(shí)現(xiàn)文字掉落效果的方法,可實(shí)現(xiàn)文字往下掉落最終排序正常顯示的效果,具有一定參考借鑒價值,需要的朋友可以參考下
本文實(shí)例講述了JS實(shí)現(xiàn)文字掉落效果的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<!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的當(dāng)前距離
var disPerFreqS=[];//每個dom的
var targetDis=500;
var domCssTopS=[];//所有dom的top屬性
var successDom=[];//記錄哪些dom已經(jīng)結(jié)束運(yùn)動
var successCount=0;//有多少個dom已經(jīng)結(jié)束
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程序設(shè)計有所幫助。
相關(guān)文章
js實(shí)現(xiàn)仿愛微網(wǎng)兩級導(dǎo)航菜單效果代碼
這篇文章主要介紹了js實(shí)現(xiàn)仿愛微網(wǎng)兩級導(dǎo)航菜單效果代碼,通過javascript自定義函數(shù)結(jié)合鼠標(biāo)點(diǎn)擊事件實(shí)現(xiàn)tab切換的功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
微信小程序?qū)嵺`之動態(tài)控制組件的顯示/隱藏功能
這篇文章主要介紹了微信小程序?qū)嵺`之動態(tài)控制組件的顯示/隱藏功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
JavaScript對象數(shù)組排序?qū)嵗椒\析
這篇文章主要介紹了JavaScript對象數(shù)組排序?qū)嵗椒\析的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06
js實(shí)現(xiàn)旋轉(zhuǎn)木馬輪播圖效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)旋轉(zhuǎn)木馬輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-01-01
javascript實(shí)現(xiàn)倒計時跳轉(zhuǎn)頁面
本文給大家介紹了如何使用javascript實(shí)現(xiàn)倒計時跳轉(zhuǎn)到其他頁面的方法以及實(shí)現(xiàn)原理,非常的簡單實(shí)用,有需要的小伙伴可以參考下。2016-01-01
解決layui中onchange失效以及form動態(tài)渲染失效的問題
今天小編就為大家分享一篇解決layui中onchange失效以及form動態(tài)渲染失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js實(shí)現(xiàn)截取某個字符串前面的內(nèi)容
這篇文章主要介紹了js實(shí)現(xiàn)截取某個字符串前面的內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

