JS動畫之速度動畫和透明度變化

來源:互聯網
上載者:User

標籤:

一、運動架構實現思路:
1.速度(改變值left、right、width、height、opacity)
2.緩衝運動
3.多物體運動
4.任意值變動
5.鏈式運動
6.同時運動

二、勻速運動:
1、設定定時器,每隔一段時間已變更位元置,達到勻速運動
2、設定定時器前需清除定時器,以防多次觸發重複產生多個定時器
3、當運動位置達到目標值時,可通過清除定時器停止運動
4、當定義函數多處相同時,可封裝為一個函數,用不同參數調用,盡量少傳遞相同的參數
        window.onload = function(){
            var oDiv = document.getElementById("div1");
            oDiv.onmouseover = function(){
                startMove(0);
            }
            oDiv.onmouseout = function(){
                startMove(-200);
            }
        }
        var timer=null;
        function startMove(iTarget){
            clearInterval(timer);
            var oDiv = document.getElementById("div1");
            timer=setInterval(function(){
                var speed=0;
                if(oDiv.offsetLeft>iTarget){
                    speed=-10;
                }
                else{speed=10;}
                if(oDiv.offsetLeft==iTarget){
                    clearInterval(timer);
                }
                else{
                oDiv.style.left=oDiv.offsetLeft+speed+"px";
                }
            },30)
        }

三、透明動畫
// #div1{
//     width:200px;
//     height:200px;
//     background:red;
//     所有主流瀏覽器(IE,Firefox,Opera,Chrome,Safari)都支援opacity屬性
// 注意:IE8和早期版本支援另一種過濾器屬性。像:filter:Alpha(opacity=50)
//     filter:alpha(opacity:30);
//     opacity:0.3;
// }
        window.onload=function(){
            var oDiv=document.getElementById("div1");
            oDiv.onmouseover=function(){
                startMove(100);
            }
            oDiv.onmouseout=function(){
                startMove(30);
            }
        }
        var timer=null;
        var alpha=30;
        function startMove(iTarget){
            var oDiv=document.getElementById("div1");
            clearInterval(timer);
            timer=setInterval(function(){
                var speed=0;
                if(alpha>iTarget){
                    speed=-10;
                }
                else{
                    speed=10;
                }
                if(alpha==iTarget){
                    clearInterval(timer);
                }
                else{
                    alpha+=speed;
                    oDiv.style.filter="alpha(opacity:"+alpha+")";
                    oDiv.style.opacity=alpha/100;
                }
            },30)
        }











JS動畫之速度動畫和透明度變化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.