HTML+JavaScript實現鏈式運動特效,javascript鏈式

來源:互聯網
上載者:User

HTML+JavaScript實現鏈式運動特效,javascript鏈式

在學習js的過程中,發現這傢伙做特效真是不錯,雖然說目前水平還不夠,不過也能寫點簡單的效果。今天分享一個簡單的運動架構,然後利用這個架構實現簡單的鏈式運動特效。

1.move.js

在運動架構中,主要進行長寬變化和透明度的變化。長寬的變化可以實現DIV的體積運動變化效果,透明度的話主要是在滑鼠移入移出事件中添加淡入淡出的效果。我將這個簡單的運動架構封裝到一個單獨的js檔案中,方便調用。

先看看代碼:

<span style="font-family:KaiTi_GB2312;">//用於擷取非行間樣式</span>function getStyle(obj,name){if(obj.currentStyle){return obj.currentStyle[name];}else{return getComputedStyle(obj,false)[name];}}<span style="font-family:KaiTi_GB2312;">//四個參數分別為對象,屬性,修改值,函數</span>function startMove(obj,attr,iTarget,func){clearInterval(obj.timer);obj.timer=setInterval(function(){var cur=0;<span style="font-family:KaiTi_GB2312;">//判斷要變化的屬性是否為透明度</span>if(attr=='opacity'){cur=parseFloat(getStyle(obj,attr)*100);}else{cur=parseInt(getStyle(obj,attr));}//<span style="font-family:KaiTi_GB2312;">運動速度計算</span>var speed=(iTarget-cur)/6;speed=speed>0?Math.ceil(speed):Math.floor(speed);if(cur==iTarget){clearInterval(obj.timer);                        <span style="font-family:KaiTi_GB2312;">                             //若存在函數參數,則執行函數</span>if(f<span style="font-family:KaiTi_GB2312;">unc</span>){<span style="font-family:KaiTi_GB2312;">         </span>f<span style="font-family:KaiTi_GB2312;">unc</span>();} }else{ if(attr=='opacity'){<span style="font-family:KaiTi_GB2312;">//保證IE和FF的相容性</span><span style="font-family:KaiTi_GB2312;">                                      </span>obj.style.filter='alpha(opacity:'+(cur+speed)+')';obj.style.opacity=(cur+speed)/100;}else{obj.style[attr]=cur+speed+'px';}}},30);}

2.index.html

<html><head><meta charset="utf-8"><style>#div1{width:100px;height:100px;background:red;filter:alpha(opacity:30);opacity:0.3;}</style><script src="move.js"></script><script>window.onload=function(){var oDiv=document.getElementById('div1');oDiv.onmouseover=function (){startMove(oDiv,'width',300,function(){startMove(oDiv,'height',300,function(){startMove(oDiv,'opacity',100);});});}; oDiv.onmouseout=function (){startMove(oDiv,'opacity',30,function(){startMove(oDiv,'height',100,function(){startMove(oDiv,'width',100);});});};};</script></head><body><div id="div1"></div></body></html>
在mouseover中:先將DIV進行width變化,等運動結束。然後進行height變化,最後進行opacity變化。

在mouseout中:先進行opacity變化,然後是height變化,最後進行width變化。就是和移入事件運動順序完全相反。(此執行個體並無直接應用意義,但是對運動架構進行一些完善之後就可以做一些常見的特效了)

在startMove()的使用過程中,有類似與遞迴調用的感覺,不過這個好理解多了。

3.小結

雖然說運動架構只有幾十行的代碼,但是其中的細節還是很多的。如果有用到這個架構的同學,在使用的時候先好好整理代碼的邏輯,要不然出了bug,也真夠虐心的。還好,我把注釋寫的很清楚了,希望能協助到有需要的人。

上兩張運動前後的圖:

(忽略浮水印)

具體運動過程還是自己實踐一番為好,再稍作修飾的話,效果肯定倍加。


用javascript與html怎製作省市級聯特效?

1 兩個下拉選擇框控制項
2 在第一個下拉選擇框控制項的change事件中添加相應代碼改變第二個控制項的值
 
用JAVASCRIPT實現HTML頁面

你好,你看一看是不是這樣不是的話我再改:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
</head>

<body><center>
<h3>頁面標題</h3>
<hr width="80%"/>
<form name="form1" method="post" action="">
<table width="509" border="1" cellspacing="0" cellpadding="0">
<caption>
表頭
</caption>
<tr>
<td width="171" align="center">姓名</td>
<td colspan="2" align="center">連絡方式</td>
</tr>
<tr>
<td align="center"><label>
<input type="text" name="textfield">
</label></td>
<td width="85" align="right">固定電話</td>
<td width="245"><input type="text" name="textfield2"></td>
</tr>
<tr>
<td align="center"> </td>
<td align="right">手機</td>
<td><input type="text" name="textfield3"></td>
</tr>
</table>
</form>
</center></body>
</html>...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.