码迷,mamicode.com
首页 > Web开发 > 详细

用JS 做的 动画 带停止和开始

时间:2017-03-02 23:55:34      阅读:679      评论:0      收藏:0      [点我收藏+]

标签: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 做的 动画 带停止和开始

标签:ext   interface   cti   out   doc   value   app   error   ack   

原文地址:http://www.cnblogs.com/lianyuewuxia/p/6492793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!