Site icon บริษัท โค๊ดบี จำกัด

วิธีทำ html animation ด้วย greensock tween js

<p><strong>วิธีทำ html animation ด้วย greensock tween js<&sol;strong><&sol;p>&NewLine;<p>ตัวอย่าง <a href&equals;"http&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;examples&sol;greensock-tween-js&sol;" title&equals;"ตัวอย่างการทำ html animation &&num;038&semi; movement" target&equals;"&lowbar;blank">http&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;examples&sol;greensock-tween-js&sol;<&sol;a><&sol;p>&NewLine;<p>Greensock Tween เป็น tween engine ที่แต่เดิมเกิดจากภาษา action script ที่ใช้สำหรับเขียนโปรแกรมบนแฟรช<br &sol;>&NewLine;ต่อมาได้ถูกพัฒนาให้ใช้งานได้กับภาษา javascript จึงเรียกว่า greensock tween js<br &sol;>&NewLine;<&excl;--more--><&sol;p>&NewLine;<p>จุดเด่นของ Greensock tween คือ ใช้งานง่าย มีความหลากหลายและยืดหยุ่นในการใช้งานมาก ๆ<br &sol;>&NewLine;และที่สำคัญ สามารถทำไปใช้งานได้แบบฟรี ๆ ไม่มีค่าใช้จ่าย<&sol;p>&NewLine;<p><strong>เริ่มใช้งาน Greensock Tween <&sol;strong><&sol;p>&NewLine;<p>1&period; download greensock tween js ได้ที่ <a href&equals;"https&colon;&sol;&sol;greensock&period;com" title&equals;"greensock tween js" target&equals;"&lowbar;blank">https&colon;&sol;&sol;greensock&period;com<&sol;a><&sol;p>&NewLine;<p>2&period; สร้างไฟล์ index&period;html ขึ้นมาครับแล้วทำการเรียกใช้งาน TweenMax&period;min&period;js และ ไฟล์ jquery&period;min&period;js<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" >&lt&semi;script type&equals;"text&sol;javascript" src&equals;"js&sol;greensock&sol;TweenMax&period;min&period;js"&gt&semi;&lt&semi;&sol;script&gt&semi; &NewLine;&lt&semi;script src&equals;"https&colon;&sol;&sol;ajax&period;googleapis&period;com&sol;ajax&sol;libs&sol;jquery&sol;1&period;11&period;1&sol;jquery&period;min&period;js"&gt&semi;&lt&semi;&sol;script&gt&semi;<&sol;pre>&NewLine;<p>3&period; ต่อไปเราจะลองทำ tween ให้กับรูปดูนะครับ สร้าง tag img มาใน body และตั้งชื่อ class ว่า myimg<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" ><img class&equals;"myimg" src&equals;"greensock-tween-js&period;png"&sol;><&sol;pre>&NewLine;<p>4&period; เขียนคำสั่ง javascript ให้คลาส myimg มี rotation หมุนไปที่ 50 องศา และกลับมาที่ 0 องศาเหมือนเดิม<br &sol;>&NewLine; จะได้โค๊ดประมาณนี้ครับ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" ><script> &NewLine;start&lpar;&rpar;&semi; &NewLine;function start&lpar;&rpar; &NewLine;&lbrace; &NewLine;&Tab;TweenLite&period;to&lpar;&dollar;&lpar;"&period;myimg"&rpar;&comma; 1&comma; &lbrace;css&colon;&lbrace;rotation&colon;50&rcub;&comma;onComplete&colon;repeat&rcub;&rpar;&semi; &Tab; &NewLine;&rcub; &NewLine;function repeat&lpar;&rpar; &NewLine;&lbrace; &NewLine;&Tab;TweenLite&period;to&lpar;&dollar;&lpar;"&period;myimg"&rpar;&comma; 1&comma; &lbrace;css&colon;&lbrace;rotation&colon;-50&rcub;&comma;onComplete&colon;start&rcub;&rpar;&semi; &Tab; &NewLine;&rcub; &NewLine; &NewLine;<&sol;script><&sol;pre>&NewLine;<p>หรือถ้าต้องการให้คลาส myimg หมุนแบบต่อเนื่อง ให้เราเปลี่ยนไปใช้ TweenMax และใช้<br &sol;>&NewLine;property repeat ของ TweenMax เข้ามาช่วย จะได้คำสั่งประมาณนี้ครับ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" > &NewLine;start&lpar;&rpar;&semi; &NewLine;function start&lpar;&rpar; &NewLine;&lbrace; &NewLine;&Tab;TweenMax&period;to&lpar;&dollar;&lpar;"&period;myimg"&rpar;&comma; 1&comma; &lbrace;repeatDelay&colon;0&comma;repeat&colon;10&comma;css&colon;&lbrace;rotation&colon;360&rcub;&rcub;&rpar;&semi; &Tab; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>เราสามารถเพิ่มรูปแบบของการ tween ได้ด้วยการใส่ property ease ให้กับ tween<br &sol;>&NewLine;เริ่มโดยการเรียกใช้ EasePack&period;min&period;js<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" > &NewLine;<script type&equals;"text&sol;javascript" src&equals;"js&sol;greensock&sol;EasePack&period;min&period;js"><&sol;script> &NewLine;<&sol;pre>&NewLine;<p>เพิ่มรูปแบบการทวีน<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" > &NewLine;TweenMax&period;to&lpar;&dollar;&lpar;"&period;myimg"&rpar;&comma; 1&comma; &lbrace;repeatDelay&colon;0&comma;repeat&colon;10&comma;css&colon;&lbrace;rotation&colon;360&rcub;&comma;ease&colon;Back&period;easeInOut&rcub;&rpar;&semi; &Tab; &NewLine;<&sol;pre>&NewLine;<p><strong>สรุปการใช้งาน<&sol;strong><br &sol;>&NewLine;จากตัวอย่างจะใช้งานแค่ rotation เราสามารถ tween ตำแหน่ง x&comma;y หรือ scaleX&comma; scaleY ได้ด้วยนะครับ<br &sol;>&NewLine;หรือแม้แต่ CSS properties พวก background-color&comma; opacity และอื่น ๆ ก็สามารถทวีนได้เช่นกันครับ<&sol;p>&NewLine;<p>ตัวอย่าง <a href&equals;"http&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;examples&sol;greensock-tween-js&sol;" title&equals;"ตัวอย่างการทำ html animation &&num;038&semi; movement" target&equals;"&lowbar;blank">http&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;examples&sol;greensock-tween-js&sol;<&sol;a><&sol;p>&NewLine;<p><strong>โค๊ดที่เสร็จแล้ว<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " title&equals;"วิธีใช้ greensock tween js" > &NewLine;<html> &NewLine;<head> &NewLine;<title>Greensock Tween JS<&sol;title> &NewLine; &NewLine;<script type&equals;"text&sol;javascript" src&equals;"js&sol;greensock&sol;TweenMax&period;min&period;js"><&sol;script> &NewLine;<script src&equals;"https&colon;&sol;&sol;ajax&period;googleapis&period;com&sol;ajax&sol;libs&sol;jquery&sol;1&period;11&period;1&sol;jquery&period;min&period;js"><&sol;script> &NewLine; &NewLine;<&sol;head> &NewLine;<style> &NewLine;body&lbrace; &NewLine;&Tab;text-align&colon;center&semi; &NewLine;&Tab;margin-top&colon;100px&semi; &NewLine;&rcub; &NewLine;<&sol;style> &NewLine;<body> &NewLine;<img width&equals;"300" class&equals;"myimg" src&equals;"greensock-tween-js&period;png"&sol;> &NewLine;<&sol;body> &NewLine; &NewLine;<script> &NewLine;start&lpar;&rpar;&semi; &NewLine;function start&lpar;&rpar; &NewLine;&lbrace; &NewLine;&Tab;TweenLite&period;to&lpar;&dollar;&lpar;"&period;myimg"&rpar;&comma; 1&comma; &lbrace;css&colon;&lbrace;rotation&colon;50&rcub;&comma;onComplete&colon;repeat&rcub;&rpar;&semi; &Tab; &NewLine;&rcub; &NewLine;function repeat&lpar;&rpar; &NewLine;&lbrace; &NewLine;&Tab;TweenLite&period;to&lpar;&dollar;&lpar;"&period;myimg"&rpar;&comma; 1&comma; &lbrace;css&colon;&lbrace;rotation&colon;-50&rcub;&comma;onComplete&colon;start&rcub;&rpar;&semi; &Tab; &NewLine;&rcub; &NewLine; &NewLine;<&sol;script> &NewLine;<&sol;html> &NewLine;<&sol;pre>&NewLine;

Exit mobile version