利用TweenMax針對HTML頁面製作動畫 – 基礎篇

馬老師在前一篇文章介紹過GreenSock的Tweening Platform在Flash中的應用後,接下來要跟各位分享「GreenSock Animation Platform」(GSAP),那麼多英文聽起來好像很複雜,就讓我為各位簡單介紹一下吧!

首先GreenSock一開始真的是針對Flash的Tween(補間)寫了許多好用的Class供設計師們使用,在不斷的更新和強化之後,目前已經出到v11(第十一版)了,但只要熟悉網路平台的朋友應該都知道,近幾年Flash在網頁上的應用,除了遊戲之外正在減少中,所以在GreenSock的v12(第十二版)中,加入了JavaScript的Class,也就是說我們可以在HTML網頁中使用TweenLite或TweenMax…等來製作動畫,也藉此版本把發展計劃的名稱從「GreenSock Tweening Platform」(GreenSock補間平台)更名為「GreenSock Animation Platform」(GreenSock動畫平台),主要是把「Tweening」換成「Animation 」,因為前者主要為Flash在使用的名詞,而後者就屬於比較廣義的「動畫」,所以未來就算你還在使用Flash的TweenMax,他也是屬於「GreenSock Animation Platform」中的一員了。

在網頁上GreenSock目前提供使用的有「TweenLite」、「TweenMax」、「TimelineLite」、「TimelineMax」這四個項目,雖比Flash來的少,不過也是最常用的幾項,相信可以幫助設計師們在不使用Flash的情形之下,製作許多不同凡響的動畫效果,另外關於這四項的差別也跟Flash版本一樣,之前有介紹過在這邊就不多提了,接下來就看一下簡單的使用方式吧。

首先跟Flash版本一樣,必須先下載備用載點)JS的Class,下載解壓縮後,會有以下的資料夾:

  • docs:參考文件。
  • examples:裡面有很多html的展示,對於想要了解本Class非常有幫助。
  • src:主要Class資料夾。

在網頁上主要使用的資料夾為src,而若是網頁要使用這個Class,跟Flash需要先import,不過HTML的指令如下:

<script src="src/minified/TweenMax.min.js"></script>

接下來就開始針對HTML和Javascript進行編輯,先看看我製作的第一個範例,下面是本範例整個網頁的程式碼:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>GreenSock HTML Tweening Basic</title>
    <script src="src/minified/TweenMax.min.js"></script>
    <script language="javascript">
        function scaleSmall() {
            var photo = document.getElementById("cat");
            TweenMax.to(photo, 2, {
                width: 300,
                height: 200
            });
        }
    </script>
</head>
<body style="font-size:13px; background-color:#FFF">
    <p style="text-align:center"><img src="cat.jpg" alt="波妞 大頭照" name="cat" width="600" height="400" id="cat"
            onClick="scaleSmall()"></p>
    <p style="text-align:center">請點擊貓咪照片</p>
</body>
</html>

基本上就是點擊後圖片縮小的效果,第10行即為TweenMax製作動畫的程式,當然利用TweenMax製作動畫,其中動畫類型也是重要的參數,第二個案例就加上了動畫類型,大家可以參考動畫變化的形式,下面是本範例整個網頁的程式碼:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>GreenSock HTMLTweening ease</title>
    <script src="src/minified/TweenMax.min.js"></script>
    <script language="javascript">
        function scaleSmall() {
            var photo = document.getElementById("cat");
            TweenMax.to(photo, 2, {
                width: 300,
                height: 200,
                ease: Elastic.easeOut
            });
        }
    </script>
</head>
<body style="font-size:13px; background-color:#FFF">
    <p style="text-align:center"><img src="cat.jpg" alt="波妞 大頭照" name="cat" width="600" height="400" id="cat"
            onClick="scaleSmall()"></p>
    <p style="text-align:center">請點擊貓咪照片</p>
</body>
</html>

除此之外,動畫變化的大小,當然也可以利用比例來計算,第三個範例就是用圖片寬度和高度比例來進行動畫縮小,各位也可以參考,下面是本範例整個網頁的程式碼:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>GreenSock HTMLTweening ease</title>
    <script src="src/minified/TweenMax.min.js"></script>
    <script language="javascript">
        function scaleSmall() {
            var photo = document.getElementById("cat");
            TweenMax.to(photo, 2, {
                width: photo.width * 0.8,
                height: photo.height * 0.8,
                ease: Elastic.easeOut
            });
        }
    </script>
</head>
<body style="font-size:13px; background-color:#FFF">
    <p style="text-align:center"><img src="cat.jpg" alt="波妞 大頭照" name="cat" width="600" height="400" id="cat"
            onClick="scaleSmall()"></p>
    <p style="text-align:center">請連續點擊貓咪照片</p>
</body>
</html>

當然在原先TweenMax可以使用的參數「onStart」、「onUpdate」、「onComplete」也一樣可以使用,第四個範例就加上了這部分的程式碼,下面是本範例整個網頁的程式碼:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>GreenSock HTMLTweening onCompleteSock HTMLTweening onComplete</title>
    <script src="src/minified/TweenMax.min.js"></script>
    <script language="javascript">
        function scaleSmall() {
            var photo = document.getElementById("cat");
            TweenMax.to(photo, 2, {
                width: 300,
                height: 200,
                ease: Elastic.easeOut,
                onComplete: okFn
            });
        }

        function okFn() {
            alert("動畫執行完畢");
        }
    </script>
</head>
<body style="font-size:13px; background-color:#FFF">
    <p style="text-align:center"><img src="cat.jpg" alt="波妞 大頭照" name="cat" width="600" height="400" id="cat"
            onClick="scaleSmall()"></p>
    <p style="text-align:center">請點擊貓咪照片</p>
</body>
</html>

看完了以上的範例,大家對於TweenMax在網頁中的使用應該有基本的認識,當然接下來就要看你的創意和HTML、Javascript、CSS的操作能力了,在這邊也製作一個較完整的範例供大家參考,下面是本範例整個網頁的程式碼:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>GreenSock HTMLTweening Sample 1</title>
    <style type="text/css">
        #yahoo {
            height: 240px;
            width: 560px;
            margin-right: auto;
            margin-left: auto;
            overflow: hidden;
        }

        /* yahoo Div 樣式(最外層) */
        #yahoo #pic {
            height: 210px;
            width: 2240px;
            overflow: hidden;
            left: 0px;
            position: relative;
        }

        /* 放置圖片Div樣式 */
        #yahoo #btn {
            text-align: right;
            float: left;
            height: 30px;
            width: 560px;
        }

        /* 下方文字連結Div */
        #yahoo #btn a {
            margin-left: 5px;
            padding-top: 2px;
            padding-right: 4px;
            padding-bottom: 2px;
            padding-left: 4px;
        }

        /* 下方文字連結樣式 */
    </style>
    <script src="src/minified/TweenMax.min.js"></script>
    <script language="javascript">
        function moveFn(no) {
            var banner = document.getElementById("pic");
            TweenMax.to(banner, 1, {
                css: {
                    left: (no - 1) * -560
                },
                ease: Quad.easeInOut
            });
        }
    </script>
</head>
<body style="font-size:13px; background-color:#FFF">
    <div id="yahoo">
        <div id="pic"><img src="yahoo/y1.jpg" id="p1" width="560" height="210"><img src="yahoo/y2.jpg" width="560"
                height="210"><img src="yahoo/y3.jpg" width="560" height="210"><img src="yahoo/y4.jpg" width="560"
                height="210"></div>
        <div id="btn"><a href="javascript:;" onClick="moveFn(1)">1</a><a href="javascript:;" onClick="moveFn(2)">2</a><a
                href="javascript:;" onClick="moveFn(3)">3</a><a href="javascript:;" onClick="moveFn(4)">4</a></div>
    </div>
</body>
</html>

在網頁上有TweenMax協助我們製作動畫,我們更可以把心思放在排版、樣式和內容上面,省去了撰寫計算動畫程式的時間,希望大家可以利用它做出很棒的作品!最後附上本範例的原始檔給大家參考。

You may also like...

4,414 Responses

  1. When someone writes an piece of writing he/she maintains the image of a user in his/her
    mind that how a user can understand it. Thus that’s why
    this paragraph is amazing. Thanks!

  2. I have been exploring for a little bit for any high-quality articles
    or blog posts in this kind of space . Exploring in Yahoo I eventually stumbled upon this web site.
    Reading this info So i’m happy to exhibit that I have an incredibly
    good uncanny feeling I discovered exactly what I needed.
    I so much certainly will make sure to don?t fail to remember this
    site and give it a glance on a relentless basis.

  3. This is a very good tip especially to those fresh to the blogosphere. Brief but very accurate info… Appreciate your sharing this one. A must read post!

  4. JamesBak表示:

    can you buy cheap clomid without rx: can i buy generic clomid pills – can you get clomid without dr prescription

  5. 博彩论坛表示:

    Howdy! This article couldn’t be written much better! Looking through this post reminds me of my previous roommate! He continually kept talking about this. I most certainly will forward this article to him. Fairly certain he will have a great read. Thank you for sharing!

  6. This is a topic that’s near to my heart… Thank you! Exactly where can I find the contact details for questions?

  7. Wrpqqa表示:

    speman online buy – cheap finasteride sale cheap finasteride

  8. dokuma şerit表示:

    dokuma şerit

    I’m not that much of a online reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back in the future.
    All the best

  9. I really love your blog.. Great colors & theme. Did you develop this site yourself? Please reply back as I’m planning to create my own personal website and would like to know where you got this from or exactly what the theme is called. Many thanks!

  10. JamesBak表示:

    buy cipro without rx: buy cipro – buy cipro online canada

  11. Dktpoh表示:

    speman sale – finasteride tablet finasteride price

  12. Hello there! I could have sworn I’ve been to this website before but after going through some of the posts I realized it’s new to me. Nonetheless, I’m definitely pleased I came across it and I’ll be book-marking it and checking back regularly.

  13. JamesBak表示:

    amoxicillin 500 mg tablets: amoxicillin 775 mg – amoxicillin medicine

  14. This website was… how do you say it? Relevant!! Finally I’ve found something that helped me. Appreciate it!

  15. orjinal toner表示:

    I will immediately grab your rss as I can not find your email subscription hyperlink or newsletter service.
    Do you have any? Kindly permit me realize so
    that I may just subscribe. Thanks.

  16. so much great information on here, : D.

  17. Hello there! This blog post could not be written any better! Looking at this article reminds me of my previous roommate! He always kept talking about this. I’ll send this post to him. Pretty sure he will have a good read. Thanks for sharing!

  18. kubet77表示:

    An outstanding share! I have just forwarded this onto a co-worker who was conducting a little homework on this. And he actually ordered me dinner due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to discuss this matter here on your web page.

  19. JamesBak表示:

    prescription for amoxicillin: amoxicillin 750 mg price – cheap amoxicillin 500mg

  20. Limo Tour表示:

    Everyone loves it when individuals come together and share views. Great website, continue the good work!

  21. iptv表示:

    I needed to thank you for this great read!! I certainly enjoyed every bit of it. I’ve got you book-marked to look at new stuff you post…

  22. iptv表示:

    I have to thank you for the efforts you have put in writing this blog. I’m hoping to view the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has encouraged me to get my very own blog now 😉

  23. JamesBak表示:

    get clomid online: how can i get generic clomid price – can i order generic clomid without insurance

  24. iptv表示:

    I want to to thank you for this fantastic read!! I absolutely loved every little bit of it. I have you saved as a favorite to check out new things you post…

  25. This is a topic that’s close to my heart… Many thanks! Where are your contact details though?

  26. ngentot表示:

    This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me. Thanks a lot.

  27. JamesBak表示:

    ciprofloxacin over the counter: ciprofloxacin generic – ciprofloxacin over the counter

  28. ngentot表示:

    That is a great tip particularly to those fresh to the blogosphere. Simple but very precise info… Thanks for sharing this one. A must read article!

  29. Flum float表示:

    Wonderful post! We will be linking to this great content on our site. Keep up the good writing.

  30. Uqgebm表示:

    buy generic gasex over the counter – order ashwagandha diabecon pills

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。