網路上現成的 JS 動畫庫不少,但功能越完整、程式碼就越龐大,动辄上千行,放進小專案顯得不划算。於是自己封裝了一個極輕量的版本,把常用能力都涵蓋進去,難免仍有疏漏,歡迎指教。
anim(elemId, cssObj, time, animType, funObj)
{ marginLeft: '200px', top: '200px', borderWidth: '8px' }。{ start: function (el) { el.innerHTML = 'start!'; }, complete: function (el) { el.innerHTML = 'Completed!'; } },參數 el 即 elemId 指向的元素。position: absolute)元素上,務必注意 CSS 的設定方式。絕對定位元素應該設 top 而不是 marginTop,更不該兩者混用——它們的參考點不同,同時設定很容易造成混亂,因此本庫不支援同時設定兩者。left/marginLeft、right/marginRight 同理,同時設定 top 與 bottom、left 與 right 也不支援。
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<style type="text/css">
#container { border: 1px solid #000; width: 500px; height: 400px; }
#aa { border: 1px solid #000; width: 100px; height: 40px;
background-color: #0f0; position: absolute; left: 50px; top: 50px; }
#bb { border: 1px solid #000; width: 500px; height: 40px; margin-top: 100px; }
#cc { border: 1px solid #000; width: 500px; height: 40px; }
</style>
</head>
<body>
<input id="Abegin" type="button" value="開始" />
<input id="Apause" type="button" value="暫停" />
<input onclick="location.reload()" type="button" value="刷新網頁" />
</body>
</html>
設計這個庫時刻意保留了「不支援的清單」,因為把邊界寫清楚,比默默做出錯誤行為要好。動畫隊列與更完整的屬性對照,是後續可以再補的方向。