用JS 做的 動畫 帶停止和開始

來源:互聯網
上載者:User

標籤:ext   interface   cti   out   doc   value   app   error   ack   

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<div id="showImg" style="width:320px; height:375px;"></div>
<button id="start">開始</button>
<button id="stop">停止</button>
<input type="text" value="200" id="speed" />
</body>
</html>
<script type="text/javascript">
 

var FBFAnimation = (function(){
    function start() {
        if (this.timeLine) return;
        var speed = (this._json.speed ? this._json.speed : 500), bthis = this;
        this.timeLine = setInterval(function() {
            animation.apply(bthis);
        }, speed);
    };
 
    function stop() {
        if (this.timeLine) clearTimeout(this.timeLine);
        this.timeLine = null;
    };
 
    function setSpeed(val) {
        if (!val) return;
        this._json.speed = val;
    };
 
    function getPosition() {
        var x = 0, y = 0;
        if ((this.startFrames == this.endFrames) && this._json.loop) this.startFrames = 0;
        x = -(this.width * this.startFrames);
 
        this.startFrames++;
        return { x : x, y : y };
    };
 
    function animation() {
        if (!this._json.loop && this.startFrames == this.endFrames - 1) this.stop();
        var offset = getPosition.apply(this);
        this._json.target.style.backgroundPosition = ""+ offset.x +"px "+ offset.y +"px";
    };
 
    function init() {
        var target = this._json.target;
        this.width = 0;
        this.height = 0;
        this.startFrames = 0;
        this.endFrames = this._json.frames;
        target.style.backgroundImage = ‘url(‘ + this._json.img + ‘)‘;
        target.style.backgroundRepeat = "no-repeat";
        target.style.backgroundPosition = "0 0";
 
        if (!this._json.width) this.width = parseInt(this._json.target.style.width);
        if (!this._json.height) this.height = parseInt(this._json.target.style.height);
    };
 
    return function(args) {
        this._json = args || {};
 
        if (!this._json.target) throw new Error(‘target is not exist!‘);
        if (!this._json.img) throw new Error(‘img not exist!‘);
        if (!this._json.frames) throw new Error(‘frames number not exist‘);
        if (typeof this._json.loop == "undefined") this._json.loop = true;
 
        init.apply(this);       //initialize
 
        this.start = start;     //interface
        this.stop = stop;
        this.setSpeed = setSpeed;
    };
})();
 /*{
target:null,
img:url,
frames:number, 幀數
speed:500 hm,
loop:true,
width:null, //舞台尺寸
height:null
}*/
var s = new FBFAnimation({
    target : document.getElementById(‘showImg‘),
    img : ‘img.png‘,
    frames : 7,
    speed : 200
});
 
document.getElementById(‘start‘).onclick = function() {
    s.start();
};
 
document.getElementById(‘stop‘).onclick = function() {
    s.stop();
};
 
document.getElementById(‘speed‘).onchange = function() {
    s.setSpeed(document.getElementById(‘speed‘).value);
}
</script>

用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.