js拋物線動畫

來源:互聯網
上載者:User

標籤:

轉:http://www.cnblogs.com/wangmeijian/p/5824176.html

/**     * js拋物線動畫     * @param  {[object]} origin [起點元素]     * @param  {[object]} target [目標點元素]     * @param  {[object]} element [要運動的元素]     * @param  {[number]} a [拋物線弧度]     * @param  {[number]} time [動畫執行時間]     * @param  {[function]} callback [拋物線執行完成後回調]     */    var parabola = function(config){        var b = 0,            INTERVAL = 15,            timer = null,            x1,y1,x2,y2,originx,originy,diffx,diffy;        this.config = config || {};        // 起點        this.origin = $(this.config.origin)||null;        // 終點        this.target = $(this.config.target)||null;        // 運動的元素        this.element = $(this.config.element)||null;        // 曲線弧度        this.a = this.config.a || 0.004;        // 運動時間(ms)        this.time = this.config.time || 1000;        this.init = function(){            x1 = this.origin.offset().left;            y1 = this.origin.offset().top;            x2 = this.target.offset().left;            y2 = this.target.offset().top;            originx = x1;            originy = y1;            diffx = x2-x1;            diffy = y2-y1,            speedx = diffx/this.time;            // 已知a, 根據拋物線函數 y = a*x*x + b*x + c 將拋物線起點平移到座標原點[0, 0],終點隨之平移,那麼拋物線經過原點[0, 0] 得出c = 0;            // 終點平移後得出:y2-y1 = a*(x2 - x1)*(x2 - x1) + b*(x2 - x1)            // 即 diffy = a*diffx*diffx + b*diffx;            // 可求出常數b的值            b = (diffy - this.a*diffx*diffx)/diffx;            this.element.css({                left: x1,                top: y1            })            return this;        }        // 確定動畫方式        this.moveStyle = function(){            var moveStyle = ‘position‘,                testDiv = document.createElement(‘div‘);            if(‘placeholder‘ in testDiv){                [‘‘,‘ms‘,‘moz‘,‘webkit‘].forEach(function(pre){                    var transform = pre + (pre ? ‘T‘ : ‘t‘) + ‘ransform‘;                    if(transform in testDiv.style){                        moveStyle = transform;                    }                })            }            return moveStyle;        }        this.move = function(){            var start = new Date().getTime(),                moveStyle = this.moveStyle(),                _this = this;            timer = setInterval(function(){                if(new Date().getTime() - start > _this.time){                    clearInterval(timer);                    _this.element.css({                        left: x2,                        top: y2                    })                    typeof _this.config.callback === ‘function‘ && _this.config.callback(_this.element);                    return;                }                x = speedx * (new Date().getTime() - start);                y = _this.a*x*x + b*x;                if(moveStyle === ‘position‘){                    _this.element.css({                        left: x+originx,                        top: y+originy                    })                }else{                    if(window.requestAnimationFrame){                        window.requestAnimationFrame(_this.element[0].style[moveStyle] = ‘translate(‘+x+‘px,‘+y+‘px)‘);                    }else{                        _this.element[0].style[moveStyle] = ‘translate(‘+x+‘px,‘+y+‘px)‘;                    }                }            },INTERVAL)            return this;        }        this.init();    }

 

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.